티스토리 뷰
출처 : https://www.acmicpc.net/problem/14503
#include <iostream>
#include <algorithm>
using namespace std;
int N, M, K, result, direction, starti, startj;
int map[50][50];
bool visited[50][50];
int dir[4][2] = { {0,-1},{1,0},{0,1},{-1,0} };
bool finished;
void cal(int i, int j, int d)
{
if (finished) return;
visited[i][j] = true; map[i][j] = 2;
int startD;
if (d == 0) startD = 0;
else if (d == 1) startD = 3;
else if (d == 2) startD = 2;
else if (d == 3) startD = 1;
int count = 0; int direc = startD;
while (count < 4)
{
if (direc == 4) direc = 0;
int nexti = i + dir[direc][0]; int nextj = j + dir[direc][1];
if (nexti>0 && nextj >0 && !visited[nexti][nextj])
{
int zz;
if (direc == 0) zz = 3;
else if (direc == 1) zz = 2;
else if (direc == 2) zz = 1;
else if (direc == 3) zz = 0;
cal(nexti, nextj, zz);
}
direc++;
count++;
}
int lastdirec;
if (d == 0) lastdirec = 1;
else if (d == 1) lastdirec = 0;
else if (d == 2) lastdirec = 3;
else if (d == 3) lastdirec = 2;
int lasti = i + dir[lastdirec][0]; int lastj = j + dir[lastdirec][1];
if (map[lasti][lastj] == 2)
{
cal(lasti, lastj, d);
}
else
{
finished = true; return;
}
}
int main()
{
cin >> N >> M;
cin >> starti >> startj >> direction;
for (int i = 0; i < N; i++) for (int j = 0; j < M; j++)
{
cin >> map[i][j];
if (map[i][j]) visited[i][j] = true;
else visited[i][j] = false;
}
result = 0;
cal(starti, startj, direction);
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
{
if (map[i][j] == 2) result += 1;
}
}
cout << result;
return 0;
}
'알고리즘 > BOJ' 카테고리의 다른 글
[BOJ 9466] 텀 프로젝트 (0) | 2018.08.05 |
---|---|
[BOJ 14890] 경사로 (0) | 2018.08.02 |
[BOJ 14500] 테트로미노 (0) | 2018.08.02 |
[BOJ 13458] 시험감독 (0) | 2018.08.02 |
[BOJ 12100] 2048(Easy) (0) | 2018.08.02 |
- Total
- Today
- Yesterday
- 17837
- SW Expert Academy
- 17143
- 17142
- 2018 KAKAO BLIND RECRUITMENT
- boj
- DFS
- SWEA
- DP
- 17779
- 시간 복잡도
- 역량 테스트
- 백준
- 입출력
- 이차원 배열과 연산
- 새로운 게임 2
- 게리맨더링 2
- 연구소 3
- 알고리즘
- 트렌드
- 17140
- string
- hackerrank
- 미세먼지 안녕!
- 17144
- 2018 카카오 블라인드 채용
- scanf
- 삼성
- 팁
- STL
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |