-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1953.cpp
149 lines (124 loc) · 3.39 KB
/
1953.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//
// Created by 융미 on 2019-10-19.
//
//1953 탈주범 검거 swea
#include <iostream>
#include <queue>
#include <utility>
using namespace std;
#define MAX 51
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
int arr[MAX][MAX] = {0,};
int dis[MAX][MAX] = {0,};
int t, n, m, r,c, l;
int answer;
void initArr(){
for(int i = 0; i<MAX; ++i)
{
for(int j = 0; j<MAX; ++j)
{
arr[i][j] = 0;
dis[i][j] = 0;
}
}
}
void print(){
cout << endl;
for(int i = 0; i<n; ++i)
{
for(int j = 0; j<m; ++j){
cout << dis[i][j] << ' ';
}
cout << endl;
}
}
bool checkGo(int x, int y, int nx, int ny, int num){
bool check = false;
switch (num){
case 1:
check = true;
break;
case 2: //상 하
if((x + 1 == nx && y == ny) || (x-1 == nx && y == ny) ) check = true;
break;
case 3: //좌 우
if((x == nx && y + 1 == ny) || (x == nx && y -1 == ny)) check = true;
break;
case 4: //하 좌
if((x + 1 == nx && y == ny) || (x == nx && y - 1 == ny)) check = true;
break;
case 5: //상 좌
if((x - 1 == nx && y == ny) || (x == nx && y - 1 == ny)) check = true;
break;
case 6: //상 우
if((x - 1 == nx && y == ny) || (x == nx && y + 1 == ny)) check = true;
break;
case 7: //하 우
if((x + 1 == nx && y == ny) || (x == nx && y + 1 == ny)) check = true;
break;
}
return check;
}
bool checkSelf(int x, int y, int num, int nx, int ny, int nnum){
bool check= false;
if(x - 1 == nx && y == ny) //상
{
if(num == 1 || num == 2 || num == 4 || num == 7)
check = checkGo(x,y,nx,ny,nnum);
}else if(x + 1 == nx && y == ny){ //하
if(num == 1 || num == 2 || num == 5 || num == 6)
check = checkGo(x,y,nx,ny,nnum);
}else if(x == nx && y -1 == ny) { //좌
if(num == 1 || num == 3 || num == 6 || num == 7)
check = checkGo(x,y,nx,ny,nnum);
}else { //우
if(num == 1 || num == 3 || num == 4 || num == 5)
check = checkGo(x,y,nx,ny,nnum);
}
return check;
}
void bfs(){
answer = 0;
queue<pair<int,int> > q;
q.push(make_pair(r,c));
dis[r][c] = 1;
int time= 1;
while(!q.empty()){
pair<int,int> cur = q.front();
int x = cur.first;
int y = cur.second;
q.pop();
for (int i = 0; i < 4; ++i) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
if (arr[nx][ny] == 0 || dis[nx][ny] != 0) continue;
if (!checkSelf(x, y, arr[x][y], nx, ny, arr[nx][ny])) continue;
dis[nx][ny] = dis[x][y] + 1;
q.push(make_pair(nx, ny));
}
}
for(int i = 0; i<n; ++i){
for(int j = 0; j<m; ++j)
if(dis[i][j] >=1 && dis[i][j] <= l) ++answer;
}
//print();
}
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
cin >> t;
for(int tc = 1; tc<= t; ++tc){
initArr();
cin >> n >> m >> r >> c >> l;
for(int i = 0; i<n; ++i){
for(int j = 0; j<m; ++j){
cin >> arr[i][j];
}
}
bfs();
cout << '#' << tc << ' ' << answer << '\n';
}
return 0;
}