백준-16956. 늑대와 양
https://www.acmicpc.net/problem/16956
BFS적 성격을 잘 보여주는 문제이지만, 그래프라 하기엔…
백준의 출력결과만이 정답으로 나올수 있는게, 아니라 막기만 하면 무조건 가능하므로, 어렵지 않게 해결가능.
C++ 코드
#include <iostream>
using namespace std;
#define ROW 500
#define COL 500
char medow[ROW][COL];
char mx[4] = { 0, 1, 0, -1 }; //시계방향 조사
char my[4] = { 1, 0, -1, 0 };
void print(char arr[][COL], int r, int c) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cout << arr[i][j];
}
cout << "\n";
}
}
int main() {
int R = 0;
int C = 0;
scanf("%d %d\n", &R, &C);
bool defense = true;
for (int i = 0; i < R; i++) {
cin >> medow[i];
}
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
if (medow[i][j] == 'S') {
for (int a = 0, b = 0; a < 4; a++, b++) {
int new_i = i + mx[a];
int new_j = j + my[b];
if (new_i <0 || new_j <0 || new_i >R-1 || new_j >C-1) continue;
if (medow[new_i][new_j] == 'W') defense = false;
else if (medow[new_i][new_j] == 'S' || medow[new_i][new_j] == 'D') continue;
else medow[new_i][new_j] = 'D';
}
}
}
}
if (defense) cout << "1";
else cout << "0";
cout << "\n";
print(medow, R, C);
}
C# 코드
using System.Text;
using static System.Console;
class Program
{
static int[] mx = new int[4] { 0, 1, 0, -1 };
static int[] my = new int[4] { 1, 0, -1, 0 };
public static void Main(string[] args)
{
StringBuilder md = new StringBuilder();
int ROW = 500;
int COL = 500;
char[][] medow = new char[ROW][];
for (int m = 0; m < ROW; m++)
medow[m] = new char[COL];
// 행, 열 입력
string[] s = ReadLine().Split(' ');
int R = int.Parse(s[0]);
int C = int.Parse(s[1]);
bool defense = true;
// 초기 배열 받기 -> 문자열을 받아와서 한 글자씩 배열에 대입.
for (int i = 0; i < R; i++)
{
string str = ReadLine();
for(int j = 0; j< C; j++)
{
medow[i][j] = str[j];
}
}
for(int i = 0; i< R; i++)
{
for(int j = 0; j< C; j++)
{
if(medow[i][j] == 'W')
{
for (int a = 0, b = 0; a < 4; a++, b++)
{ //양 주위 시계 방향으로 조사
int new_i = i + mx[a];
int new_j = j + my[b];
if (new_i < 0 || new_j < 0 || new_i > R - 1 || new_j > C - 1) continue; //범위 배열 넘을시.
if (medow[new_i][new_j] == 'S')
{
defense = false; break;
}
else if (medow[new_i][new_j] == 'W' || medow[new_i][new_j] == 'D') continue;
else medow[new_i][new_j] = 'D';
}
}
}
}
if (defense)
{
md.Append("1\n");
//출력
for (int i = 0; i < R; i++)
{
for (int j = 0; j < C; j++)
{
md.Append(medow[i][j]);
}
md.Append("\n");
}
}
else md.Append("0\n");
Write(md);
}
}
Java 코드
import java.io.*;
import java.util.*;
public class Main {
static void print(char[][] arr){
for(int i = 0; i< arr.length; i++){
for(int j = 0; j< arr[i].length; j++){
System.out.print(arr[i][j]);
}
System.out.println();
}
}
static int[] mx = new int[]{0, 1, 0, -1};
static int[] my = new int[]{1, 0, -1, 0};
public static void main(String[] args) throws IOException {
Scanner s = new Scanner(System.in);
boolean defense = true;
int R = s.nextInt();
int C = s.nextInt();
char[][] medow = new char[R][C];
for(int i = 0; i<R; i++){
String str = s.next();
for(int j = 0; j <C; j++){
medow[i][j] = str.charAt(j);
}
}
for(int i = 0; i< R; i++){
for(int j = 0; j<C ; j++){
if(medow[i][j] == 'W') {
for(int a = 0, b = 0; a < 4; a++, b++){
int new_i = i+mx[a];
int new_j = j+my[b];
if(new_i <0 || new_i >= R || new_j < 0 || new_j >= C) continue;
if(medow[new_i][new_j] == 'S') {
defense = false; break;
}
else if(medow[new_i][new_j] == 'W' || medow[new_i][new_j] == 'D') continue;
else medow[new_i][new_j] = 'D';
}
}
}
}
if(defense) {
System.out.println("1");
print(medow);
}
else System.out.println("0");
}
}
Kotlin 코드
import java.util.*
import kotlin.math.*
var medow = Array(500, {CharArray(500, {','})})
val mx = arrayOf(0, 1, 0, -1)
val my = arrayOf(1, 0, -1, 0)
fun print(arr: Array<CharArray>, r: Int, c: Int){
for(i in 0 .. r-1){
for(j in 0 .. c-1){
print(arr[i][j])
}
println()
}
}
fun main(args: Array<String>){
val s = Scanner(System.`in`)
val R = s.nextInt()
val C = s.nextInt()
var defense = true
for(i in 0..R-1){
val str = s.next()
for(j in 0..C-1){
medow[i][j] = str[j]
}
}
for(i in 0..R-1){
for(j in 0.. C-1){
if(medow[i][j] == 'S'){
var b = -1
for(a in 0..3){
b++
var new_i = i + mx[a]
var new_j = j + my[b]
if(new_i < 0 || new_j <0 || new_i > R-1|| new_j>C-1) continue
if (medow[new_i][new_j] == 'W') defense = false;
else if (medow[new_i][new_j] == 'S' || medow[new_i][new_j] == 'D') continue;
else medow[new_i][new_j] = 'D';
}
}
}
}
if(defense) print("1")
else print("0")
println()
print(medow, R, C)
}
Python 코드
ROW = 501
COL = 501
medow = [[',' for _ in range(ROW)] for _ in range(COL)]
mx = [0,1,0,-1]
my = [1,0,-1,0]
def printf(arr, r ,c):
for i in range(0, r):
for j in range(0, c):
print(arr[i][j], end = '')
print()
R,C = map(int, input().split())
defense = True
str = []
for i in range(0, R):
str = input()
for j in range(0, C):
medow[i][j] = str[j]
for i in range(0, R):
for j in range(0, C):
if medow[i][j] == 'S':
b = -1
for a in range(0, 4):
b+=1
new_i = i + mx[a]
new_j = j + my[b]
if new_i < 0 | new_j < 0 | new_i > R -1 | new_j >C-1: continue
if medow[new_i][new_j] == 'W': defense = False
elif medow[new_i][new_j] == 'S' : continue
elif medow[new_i][new_j] == 'D':continue
else: medow[new_i][new_j] = 'D'
if defense :
print("1")
printf(medow, R, C)
else: print("0")