NOJ [1009] 连连看

来源:互联网 发布:淘宝权女友静雯 编辑:程序博客网 时间:2024/05/16 10:13

卡了n久就是因为局部变量拷到整体变量那里之后,忘了删掉局部变量。。改了n久...哭瞎

注意的是转向方向有限,另外注意输入的时候先y再x,然后暴力dfs

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define N 105char a[N][N];int vis[N][N];int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};int n, m;int x, y;int lx, ly;struct node{int x, y;int t;//zhuan xiang cishu}st, ed;int ans;bool f;void dfs(node now, int dirx){//printf("%d %d %d\n", now.x, now.y, now.t);if(f)return;if(now.x == ed.x && now.y == ed.y && now.t <= 3 && !f){ans -= 2;//printf("ans = %d\n", ans);f = 1;a[st.x][st.y] = '0';a[ed.x][ed.y] = '0';return;}if(now.t > 3)return;for(int i = 0; i < 4; i++){node nt;if(i == dirx)nt.t = now.t;elsent.t = now.t + 1;nt.x = dir[i][0] + now.x;nt.y = dir[i][1] + now.y;if(nt.x <= n + 1 && nt.y <= m + 1 && nt.x >= 0 && nt.y >= 0 && !vis[nt.x][nt.y] && (a[nt.x][nt.y] == '0' || (nt.x == ed.x && nt.y == ed.y))){vis[nt.x][nt.y] = 1; dfs(nt, i);}}}int main(){int tot;scanf("%d", &tot);while(tot--){scanf("%d%d%*c", &n, &m);ans = 0;memset(a, '0', sizeof(a));for(int i = 1; i <= n; i++){for(int j = 1; j <= m; j++){scanf("%c", &a[i][j]);if(a[i][j] != '0')ans ++;}getchar();}int cx;scanf("%d", &cx);scanf("%d%d", &ly, &lx);lx++, ly++;cx--;while(cx--){scanf("%d%d", &y, &x);x++; y++;//printf("x: %d y: %d\n", x, y);//printf("lx: %d ly: %d\n", lx, ly);if(a[x][y] == a[lx][ly] && a[x][y] != '0' && a[lx][ly] != '0'){node now;now.x = x, now.y = y, now.t = 0;st.x = x, st.y = y;ed.x = lx, ed.y = ly;for(int i = 0; i < 4; i++){memset(vis, 0, sizeof(vis));f = 0;dfs(now, i);if(f)break;}}lx = x, ly = y;}printf("%d\n", ans);}return 0;}


0 0