|
| 1 | +#include <string> |
| 2 | +#include <stdio.h> |
| 3 | +#include <math.h> |
| 4 | +#include <iostream> |
| 5 | +#include <vector> |
| 6 | +#include <algorithm> |
| 7 | +#include <map> |
| 8 | +#include <queue> |
| 9 | +#include <stack> |
| 10 | +#include <climits> |
| 11 | +#include <set> |
| 12 | + |
| 13 | +#define p(a, b) make_pair(a, b) |
| 14 | +#define SWAP(a, b, type) do { \ |
| 15 | + type temp; \ |
| 16 | + temp = a; \ |
| 17 | + a = b; \ |
| 18 | + b = temp; \ |
| 19 | +} while (0) |
| 20 | +#define INF 1000000000 |
| 21 | +#define endl '\n' |
| 22 | +#define ll long long |
| 23 | +#define RIGHT 1 |
| 24 | +#define LEFT 2 |
| 25 | +#define UP 3 |
| 26 | +#define DOWN 4 |
| 27 | +#define W 0 |
| 28 | +#define R 1 |
| 29 | +#define B 2 |
| 30 | + |
| 31 | +using namespace std; |
| 32 | + |
| 33 | +typedef struct _info{ |
| 34 | + int y; |
| 35 | + int x; |
| 36 | + int direct; |
| 37 | + int st_pos; |
| 38 | +}info; |
| 39 | + |
| 40 | +info v[11]; |
| 41 | +stack<int> MAP[13][13]; |
| 42 | +int color[13][13]; |
| 43 | + |
| 44 | +int N,K; |
| 45 | + |
| 46 | +bool move(int n){ |
| 47 | + int d = v[n].direct, y =v[n].y, x=v[n].x; |
| 48 | + |
| 49 | + // 파랑 or 끝인 경우 |
| 50 | + if(d==LEFT){ |
| 51 | + if(x-1 < 1 || color[y][x-1]==B) v[n].direct=RIGHT; |
| 52 | + }else if(d==RIGHT){ |
| 53 | + if(x+1>N || color[y][x+1]==B) v[n].direct=LEFT; |
| 54 | + }else if(d==UP){ |
| 55 | + if(y-1 < 1 || color[y-1][x]==B) v[n].direct=DOWN; |
| 56 | + }else{ |
| 57 | + if(y+1>N || color[y+1][x]==B) v[n].direct=UP; |
| 58 | + } |
| 59 | + |
| 60 | + // direction 방향으로 이동 |
| 61 | + bool flag=false; |
| 62 | + queue<int> q; |
| 63 | + stack<int> s; |
| 64 | + d = v[n].direct; |
| 65 | + int cnt = MAP[y][x].size() - v[n].st_pos; |
| 66 | + |
| 67 | + if(d==LEFT){ |
| 68 | + if(!(x-1 < 1 || color[y][x-1]==B)){ |
| 69 | + if(color[y][x-1]==R){ |
| 70 | + for(int i=0;i<cnt;i++){ |
| 71 | + q.push(MAP[y][x].top()); |
| 72 | + MAP[y][x].pop(); |
| 73 | + } |
| 74 | + |
| 75 | + while(!q.empty()){ |
| 76 | + int index = q.front(); q.pop(); |
| 77 | + MAP[y][x-1].push(index); |
| 78 | + v[index].x=x-1; |
| 79 | + v[index].st_pos = MAP[y][x-1].size()-1; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if(color[y][x-1]==W){ |
| 84 | + for(int i=0;i<cnt;i++){ |
| 85 | + s.push(MAP[y][x].top()); |
| 86 | + MAP[y][x].pop(); |
| 87 | + } |
| 88 | + |
| 89 | + while(!s.empty()){ |
| 90 | + int index = s.top(); s.pop(); |
| 91 | + MAP[y][x-1].push(index); |
| 92 | + v[index].x=x-1; |
| 93 | + v[index].st_pos = MAP[y][x-1].size()-1; |
| 94 | + } |
| 95 | + } |
| 96 | + if(MAP[y][x-1].size()>=4) flag=true; |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + if(d==RIGHT){ |
| 103 | + if(!(x+1 > N || color[y][x+1]==B)){ |
| 104 | + if(color[y][x+1]==R){ |
| 105 | + for(int i=0;i<cnt;i++){ |
| 106 | + q.push(MAP[y][x].top()); |
| 107 | + MAP[y][x].pop(); |
| 108 | + } |
| 109 | + |
| 110 | + while(!q.empty()){ |
| 111 | + int index = q.front(); q.pop(); |
| 112 | + MAP[y][x+1].push(index); |
| 113 | + v[index].x=x+1; |
| 114 | + v[index].st_pos = MAP[y][x+1].size()-1; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if(color[y][x+1]==W){ |
| 119 | + for(int i=0;i<cnt;i++){ |
| 120 | + s.push(MAP[y][x].top()); |
| 121 | + MAP[y][x].pop(); |
| 122 | + } |
| 123 | + |
| 124 | + while(!s.empty()){ |
| 125 | + int index = s.top(); s.pop(); |
| 126 | + MAP[y][x+1].push(index); |
| 127 | + v[index].x=x+1; |
| 128 | + v[index].st_pos = MAP[y][x+1].size()-1; |
| 129 | + } |
| 130 | + } |
| 131 | + if(MAP[y][x+1].size()>=4) flag=true; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + if(d==UP){ |
| 136 | + if(!(y-1 < 1 || color[y-1][x]==B)){ |
| 137 | + if(color[y-1][x]==R){ |
| 138 | + for(int i=0;i<cnt;i++){ |
| 139 | + q.push(MAP[y][x].top()); |
| 140 | + MAP[y][x].pop(); |
| 141 | + } |
| 142 | + |
| 143 | + while(!q.empty()){ |
| 144 | + int index = q.front(); q.pop(); |
| 145 | + MAP[y-1][x].push(index); |
| 146 | + v[index].y=y-1; |
| 147 | + v[index].st_pos = MAP[y-1][x].size()-1; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + if(color[y-1][x]==W){ |
| 152 | + for(int i=0;i<cnt;i++){ |
| 153 | + s.push(MAP[y][x].top()); |
| 154 | + MAP[y][x].pop(); |
| 155 | + } |
| 156 | + |
| 157 | + while(!s.empty()){ |
| 158 | + int index = s.top(); s.pop(); |
| 159 | + MAP[y-1][x].push(index); |
| 160 | + v[index].y=y-1; |
| 161 | + v[index].st_pos = MAP[y-1][x].size()-1; |
| 162 | + } |
| 163 | + } |
| 164 | + if(MAP[y-1][x].size()>=4) flag=true; |
| 165 | + } |
| 166 | + |
| 167 | + } |
| 168 | + |
| 169 | + if(d==DOWN){ |
| 170 | + if(!(y+1 > N || color[y+1][x]==B)){ |
| 171 | + if(color[y+1][x]==R){ |
| 172 | + for(int i=0;i<cnt;i++){ |
| 173 | + q.push(MAP[y][x].top()); |
| 174 | + MAP[y][x].pop(); |
| 175 | + } |
| 176 | + |
| 177 | + while(!q.empty()){ |
| 178 | + int index = q.front(); q.pop(); |
| 179 | + MAP[y+1][x].push(index); |
| 180 | + v[index].y=y+1; |
| 181 | + v[index].st_pos = MAP[y+1][x].size()-1; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + if(color[y+1][x]==W){ |
| 186 | + for(int i=0;i<cnt;i++){ |
| 187 | + s.push(MAP[y][x].top()); |
| 188 | + MAP[y][x].pop(); |
| 189 | + } |
| 190 | + |
| 191 | + while(!s.empty()){ |
| 192 | + int index = s.top(); s.pop(); |
| 193 | + MAP[y+1][x].push(index); |
| 194 | + v[index].y=y+1; |
| 195 | + v[index].st_pos = MAP[y+1][x].size()-1; |
| 196 | + } |
| 197 | + } |
| 198 | + if(MAP[y+1][x].size()>=4) flag=true; |
| 199 | + } |
| 200 | + |
| 201 | + } |
| 202 | + |
| 203 | + return flag; |
| 204 | +} |
| 205 | + |
| 206 | +void input() { |
| 207 | + cin>>N>>K; |
| 208 | + for(int i=1;i<=N;i++){ |
| 209 | + for(int j=1;j<=N;j++){ |
| 210 | + cin>>color[i][j]; |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + for(int i=1;i<=K;i++){ |
| 215 | + info tmp; |
| 216 | + int y,x,d; |
| 217 | + tmp.st_pos=0; |
| 218 | + |
| 219 | + cin>>y>>x>>d; |
| 220 | + tmp.y=y; |
| 221 | + tmp.x=x; |
| 222 | + tmp.direct=d; |
| 223 | + v[i]=tmp; |
| 224 | + MAP[y][x].push(i); |
| 225 | + } |
| 226 | + |
| 227 | +} |
| 228 | + |
| 229 | +void init() { |
| 230 | +} |
| 231 | + |
| 232 | + |
| 233 | +void solution() { |
| 234 | + int MAX=1000; |
| 235 | + |
| 236 | + for(int k=1;k<=MAX;k++){ |
| 237 | + for(int i=1;i<=K;i++){ |
| 238 | + if(move(i)){ |
| 239 | + cout<<k; |
| 240 | + return; |
| 241 | + } |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + cout<<-1; |
| 246 | +} |
| 247 | + |
| 248 | +int main() |
| 249 | +{ |
| 250 | + ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); |
| 251 | + input(); |
| 252 | + solution(); |
| 253 | + return 0; |
| 254 | +} |
0 commit comments