广搜迷之RE及迷之WA

最近做广搜的时候天天迷之RE,经过dalao@口昭寿指点,我把string数组换成了char二维数组就AC了,(然而我并不知道为什么

传送门  <——以这个题为例

#include <bits/stdc++.h>
using namespace std;
int xx[5] = {0,0,0,-1,1},yy[5] = {0,-1,1,0,0};
        //$$$##########string a[1001];//原先是这样,然后main函数中输入貌似要改一改,上周做的不清楚了,反正用的string数组,可以提交试试,迷之RE
char a[1001][1001];//这样就对了,不过我也不知道为什么 queue
<int>q; int tot[1001][1001]; int qx,qy,zx,zy; void gs(int n,int m) { while(q.empty() == 0) { int x = q.front(); q.pop(); int y = q.front(); q.pop(); for(int k = 1; k <= 4; k++) { int x2 = x + xx[k]; int y2 = y + yy[k]; if(a[x2][y2] == '.' && x2 >= 1 && y2 >= 1 && x2 <=n && y2 <= m) { q.push(x2); q.push(y2); tot[x2][y2] = tot[x][y] + 1; a[x2][y2] = ' '; } if(a[x2][y2] == '*') { tot[x2][y2] = tot[x][y] + 1; cout<<tot[x2][y2]<<endl; while(q.empty() == 0){ q.pop(); } memset(tot,0,sizeof(tot)); return ; } } } cout<<-1<<endl; return ; } int main() { while(1) { int n,m; scanf("%d%d",&n,&m); if(n == 0 && m == 0) break; getchar(); for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { scanf("%c",&a[i][j]); if(a[i][j] == '@') { qx = i; qy = j; a[i][j] = '#'; q.push(qx); q.push(qy); } } getchar(); } gs(n,m); memset(tot,0,sizeof(tot)); } return 0; }

 ps:迷之WA:

这个常识大部分dalao都应该知道,可这个问题整了我好几天,广搜题目很多为了加强数据什么的要多测啊....

很有可能是没有情空,多测不清空,爆零两行泪;

原文地址:https://www.cnblogs.com/lztzs/p/10748807.html