3.1. 15이원준 ¶
// 아래 기본 제공된 코드를 수정 또는 삭제하고 본인이 코드를 사용하셔도 됩니다.
#include <cstdio>
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(int argc, char** argv) {
/* 아래 freopen 함수는 input.txt 를 read only 형식으로 연 후,
앞으로 표준 입력(키보드) 대신 input.txt 파일로 부터 읽어오겠다는 의미의 코드입니다.
만약 본인의 PC 에서 테스트 할 때는, 입력값을 input.txt에 저장한 후 freopen 함수를 사용하면,
그 아래에서 scanf 함수 또는 cin을 사용하여 표준입력 대신 input.txt 파일로 부터 입력값을 읽어 올 수 있습니다.
또한, 본인 PC에서 freopen 함수를 사용하지 않고 표준입력을 사용하여 테스트하셔도 무방합니다.
단, Codeground 시스템에서 "제출하기" 할 때에는 반드시 freopen 함수를 지우거나 주석(//) 처리 하셔야 합니다. */
//freopen("input.txt", "r", stdin);
setbuf(stdout, NULL);
int TC;
int test_case;
scanf("%d", &TC); // cin 사용 가능
for (test_case = 1; test_case <= TC; test_case++) {
// 이 부분에서 알고리즘 프로그램을 작성하십시오.
int n;
scanf("%d", &n);
int** arr = (int**)malloc(sizeof(int*) * (n + 2));
for (int i = 0; i < (n + 2); i++){
arr[i] = (int*)malloc(sizeof(int) * (n + 2));
}
int** visit = (int**)malloc(sizeof(int*) * (n + 2));
for (int i = 0; i < (n + 2); i++){
visit[i] = (int*)malloc(sizeof(int) * (n + 2));
}
for (int i = 0; i < n + 2; i++){
for (int j = 0; j < n + 2; j++){
visit[i][j] = 0;
if (i == 0 || i == n + 1 || j == 0 || j == n + 1){
arr[i][j] = -1;
}
else{
scanf("%1d", &arr[i][j]);
}
}
}
int x = 1, y = 1;
pair<int, int> dir = make_pair(0, 1);
int ans = 0;
while (arr[x][y] != -1){
if (arr[x][y] != 0 && visit[x][y] == 0){
ans++;
visit[x][y] = 1;
}
pair<int, int> next;
switch (arr[x][y]){
case 0:
next = dir;
break;
case 1:
next.second = dir.first * -1;
next.first = dir.second * -1;
break;
case 2:
next.first = dir.second;
next.second = dir.first;
break;
}
dir = next;
x += dir.first;
y += dir.second;
}
// 이 부분에서 정답을 출력하십시오.
printf("Case #%d\n", test_case); // cout 사용 가능
printf("%d\n", ans);
}
return 0; // 정상종료 시 반드시 0을 리턴해야 합니다.
}
3.2. 박인서 ¶
#include <iostream>
int main()
{
int TC;
std::cin >> TC;
for (int e = 1; e <= TC; e++){
int n;
char a[1001][1001];
bool visit[1001][1001];
std::cin >> n;
for (int i = 0; i<n; i++){
for (int j = 0; j<n; j++){
std::cin >> a[i][j];
visit[i][j] = false;
}
}
int x = 0, y = 0, res = 0, dir = 0;
while (1){
if (x<0 || y<0 || x >= n || y >= n) break;
if (!visit[x][y] && a[x][y] != '0') res++;
visit[x][y] = true;
switch (a[x][y]){
case '1':
dir=3-dir;
break;
case '2':
if(dir%2==0) dir++;
else dir--;
break;
default:
break;
}
switch (dir){
case 0:
y++;
break;
case 1:
x++;
break;
case 2:
y--;
break;
case 3:
x--;
break;
}
}
std::cout << "Case #" << e << std::endl << res << std::endl;
}
return 0;
}
3.3. 황창재 ¶
// 아래 기본 제공된 코드를 수정 또는 삭제하고 본인이 코드를 사용하셔도 됩니다.
#include <stdio.h>
#pragma warning(disable:4996)
int main(void) {
/* 아래 freopen 함수는 input.txt를 read only 형식으로 열고, 표준입력(키보드) 대신 input.txt 로 부터 읽어오겠다는 의미의 코드입니다.
만약 본인 PC 에서 테스트 할 때는, 입력값을 input.txt에 저장한 후 freopen 함수를 사용하면
그 아래에서 scanf 함수를 사용하여 표준입력 대신 input.txt 파일로 부터 입력값을 읽어 올 수 있습니다.
또한, 본인 PC에서 freopen 함수를 사용하지 않고 표준입력을 사용하여 테스트하셔도 무방합니다.
단, Codeground 시스템에서 "제출하기" 할 때에는 반드시 freopen 함수를 지우거나 주석(//) 처리 하셔야만 합니다. */
// freopen("input.txt", "r", stdin);
setbuf(stdout, NULL);
char map[1000][1000];
int TC;
int test_case;
int N;
int i, j;
int dir;
int cnt;
scanf("%d", &TC);
for (test_case = 1; test_case <= TC; test_case++) {
// 이 부분에서 알고리즘 프로그램을 작성하십시오. 기본 제공된 코드를 수정 또는 삭제하고 본인이 코드를 사용하셔도 됩니다.
scanf("%d", &N);
for (i = 0; i<N; i++) {
scanf("%s", map[i]);
}
for (i = 0; i<N; i++) {
for (j = 0; j<N; j++) {
map[i][j] -= '0';
}
}
i = j = dir = cnt = 0;
while (i >= 0 && i < N && j >= 0 && j < N) {
if (map[i][j] * map[i][j] == 1) {
if (map[i][j] >0) {
map[i][j] = -1;
cnt++;
}
if (dir == 0) {
dir = 3;
i--;
}
else if (dir == 1) {
dir = 2;
j--;
}
else if (dir == 2) {
dir = 1;
i++;
}
else if (dir == 3) {
dir = 0;
j++;
}
}
if (map[i][j] * map[i][j] == 4) {
if (map[i][j] >0) {
map[i][j] = -2;
cnt++;
}
if (dir == 0) {
dir = 1;
i++;
}
else if (dir == 1) {
dir = 0;
j++;
}
else if (dir == 2) {
dir = 3;
i--;
}
else if (dir == 3) {
dir = 2;
j--;
}
}
if (map[i][j] == 0) {
if (dir == 0) {
j++;
}
else if (dir == 1) {
i++;
}
else if (dir == 2) {
j--;
}
else if (dir == 3) {
i--;
}
}
}
// 이 부분에서 정답을 출력하십시오.
printf("Case #%d\n", test_case);
printf("%d\n", cnt);
}
return 0; // 정상종료 시 반드시 0을 리턴해야 합니다.
}










