题目链接
题目描述
You’re trapped into a map with
n
×
m
n \times m
n×m grids, each grid has a strange water pipe.
There are six types of water pipes, denote them as ID 0 , 1 , … , 5 0,1,\dots,5 0,1,…,5.
You are located at the top of grid ( 1 , 1 ) {(1,1)} (1,1) and you want to reach the bottom of grid ( n , m ) {(n,m)} (n,m). In each step, you should travel along the pipe and move to another grid.
Before each move, you can do beautiful magic once: pick one of the degrees from { 0 , 90 , 180 , 270 } \{{0, 90, 180, 270}\} {0,90,180,270}, select any number of grids except the one you are located at, and rotate their water pipes with the same degree you pick in the clockwise direction. Note that after you step in a grid along the water pipe, you must walk along the other end of the pipe to leave this grid.
You may step in the same grid multiple times, and the direction can be different every time you enter.
Determine whether you can escape from the entrance to the exit successfully.
输入描述:
There are multiple test cases. The first line of the input contains an integer
T
(
1
≤
T
≤
10000
)
T(1 \le T \le 10000)
T(1≤T≤10000), indicating the number of test cases. For each test case:
The first line contains two integers n , m ( 2 ≤ n , m ≤ 1000 ) n,m(2 \le n,m \le 1000) n,m(2≤n,m≤1000) indicating the size of the map.
In the next n {n} n lines, there are m {m} m integers a i , j ( 0 ≤ a i , j ≤ 5 ) a_{i,j}(0 \le a_{i,j} \le 5) ai,j(0≤ai,j≤5) , indicating the ID of waterpipe in each grid.
It’s guaranteed that the sum of
n
×
m
n \times m
n×m over all test cases does not exceed
1
0
6
10^6
106 .
输出描述:
For each test case, output ‘YES’ if you can escape from the map, otherwise output ‘NO’.
If the answer is YES, then output a valid solution from the top of ( 1 , 1 ) {(1,1)} (1,1) to the bottom of ( n , m ) {(n,m)} (n,m). You should first output an integer k ( k ≤ 20 n m ) k(k \le 20nm) k(k≤20nm) indicating the length of your operations, and then output k {k} k lines to describe your escaping line.
If you try to rotate some cells, first output two integers t , d ( 0 < t ≤ n m , d ∈ { 0 , 90 , 180 , 270 } ) t,d(0 < t \le nm,d \in \{0,90,180,270\}) t,d(0<t≤nm,d∈{0,90,180,270}), indicating the number of cells and the degree you rotate, and then output 2 t {2t} 2t integers ( x 1 , y 1 , x 2 , y 2 , … , x t , y t ) (x_1,y_1,x_2,y_2,\dots,x_t,y_t) (x1,y1,x2,y2,…,xt,yt), indicating the cells you rotate. Two consecutive rotation requests are not allowed.
If you try to step into another cell, output three integers ( 0 , x , y ) {(0,x,y)} (0,x,y), indicating the next cell you pass by. You should assure that the first pair of ( x , y ) {(x,y)} (x,y) is ( 1 , 1 ) {(1,1)} (1,1), the last pair of ( x , y ) {(x, y)} (x,y) is ( n , m ) {(n, m)} (n,m), and your direction when stepping into the last cell is downward.
If there are multiple solutions, you can print any of them.
Your submission is not accepted if the total number of
(
x
,
y
)
{(x,y)}
(x,y) you output (including the cells you rotate and the cells you pass by) exceeds
20
n
m
{20nm}
20nm.
示例1
输入
2
2 2
4 5
0 1
2 2
0 1
2 3
输出
YES
5
2 90 1 1 2 1
0 1 1
0 2 1
2 180 1 2 2 2
0 2 2
NO
bfs+大模拟,细节较多。
#include<bits/stdc++.h>
using namespace std;
const int N = 1005;
const int lk[6][2] = {{0, 3},
{0, 1},
{1, 2},
{2, 3},
{1, 3},
{0, 2}};
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
struct sta {
int x, y, z, d;
} pre[N][N][4];
queue<sta> q;
int T, n, m, a[N][N];
vector<sta> ans, path;
bool bfs() {
while (!q.empty())q.pop();
q.push({0, 1, 5, 0});
while (!q.empty()) {
auto t = q.front();
q.pop(), t.d ^= 1;
int nx = t.x + dx[lk[t.z][t.d]];
int ny = t.y + dy[lk[t.z][t.d]];
if (nx == n + 1 && ny == m)return pre[nx][ny][lk[t.z][t.d] ^ 2] = t, true;
if (nx > n || nx < 1 || ny > m || ny < 1 || pre[nx][ny][lk[t.z][t.d] ^ 2].y)
continue;
pre[nx][ny][lk[t.z][t.d] ^ 2] = t;
if (a[nx][ny] < 4) {
for (int j = 0; j < 4; j++) {
if (lk[j][0] == (lk[t.z][t.d] ^ 2))
q.push({nx, ny, j, 0});
else if (lk[j][1] == (lk[t.z][t.d] ^ 2))
q.push({nx, ny, j, 1});
}
} else {
for (int j = 4; j < 6; j++) {
if (lk[j][0] == (lk[t.z][t.d] ^ 2))
q.push({nx, ny, j, 0});
else if (lk[j][1] == (lk[t.z][t.d] ^ 2))
q.push({nx, ny, j, 1});
}
}
}
return false;
}
inline void print() {
path.clear(), ans.clear();
for (sta cur = {n + 1, m, 5, 1}; pre[cur.x][cur.y][lk[cur.z][cur.d ^ 1]].x; cur = path.back())
path.push_back(pre[cur.x][cur.y][lk[cur.z][cur.d ^ 1]]);
reverse(path.begin(), path.end());
for (sta cur:path) {
if (cur.z == a[cur.x][cur.y])ans.push_back({cur.x, cur.y, 0});
else {
if (cur.z < 4)
ans.push_back({cur.x, cur.y, (cur.z - a[cur.x][cur.y] + 4) % 4 * 90});
else ans.push_back({cur.x, cur.y, (cur.z - a[cur.x][cur.y] + 2) % 2 * 90});
ans.push_back({cur.x, cur.y, 0}), a[cur.x][cur.y] = cur.z;
}
}
puts("YES");
printf("%d\n", (int) ans.size());
for (auto it:ans) {
if (!it.z)printf("0 %d %d\n", it.x, it.y);
else printf("1 %d %d %d\n", it.z, it.x, it.y);
}
}
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
scanf("%d", &a[i][j]);
for (int k = 0; k < 4; k++)
pre[i][j][k] = {0, 0, 0};
}
if (bfs()) print();
else puts("NO");
}
return 0;
}