Gym - 102028F
一眼看上去就是一个稍微复杂一点的bfs。
写起来也没遇到太大的障碍,一路顺风顺水写完了。
最后连续wa了三次,检查到头皮发麻也没看出问题。
最后抱着瞎试的心态换掉了setbuf(stdin, NULL);
。
竟然就a了…
你说getchar()
多好。
关于setbuf(stdin, NULL);
为什么不行还不清楚。
还有就是memset()
不能乱用,会超时,数组大的话老老实实的for比较好
下面上代码:
#include <bits/stdc++.h>
using namespace std;
/*
1
3 4
+---+ +---+
/ \ / \
+ +---+ +---+
\ \ / \
+ + +---+ S +
/ \ / /
+ +---+ + +
\ \ / \
+---+ +---+ +
/ /
+ +---+ + +
\ / \
+---+ +---+ T +
\ / \ /
+---+ +---+
*/
/*
1
1 2
+---+
/ \
+ S +---+
\ \
+---+ T +
\ /
+---+
*/
char in[4010][6010];
int r,c,starti,startj;
int turn[6][2]={{-2,-6},{-2,6},{-4,0},{4,0},{2,-6},{2,6}};
struct node {
int x,y,times;
node(int a,int b,int c) {
x=a,y=b,times=c;
}
node() {}
};
bool judge1(int x,int y) {
if(x<0||x>=4*r+3)
return false;
if(y<0||y>=6*c+3)
return false;
if(in[x][y]=='*')
return false;
return true;
}
bool judge2(int x,int y) {
if(in[x][y]=='-'||in[x][y]=='\\'||in[x][y]=='/')
return false;
return true;
}
int showMap() {
for(int i=0;i<4*r+3;i++) {
printf("%s\n",in[i]);
}
}
int Search() {
queue<node> q;
q.push(node(starti,startj,1));
in[starti][startj]='*';
while(!q.empty()) {
node temp=q.front();
q.pop();
if(in[temp.x][temp.y]=='T') {
return temp.times;
}
int x=temp.x,y=temp.y,times=temp.times;
for(int i=0;i<6;i++) {
if(judge1(x+turn[i][0],y+turn[i][1])&&judge2(x+turn[i][0]/2,y+turn[i][1]/2)) {
q.push(node(x+turn[i][0],y+turn[i][1],times+1));
if(in[x+turn[i][0]][y+turn[i][1]]!='T')
in[x+turn[i][0]][y+turn[i][1]]='*';
}
}
}
return -1;
}
int main() {
int t;
while(~scanf("%d",&t)) {
while(t--) {
scanf("%d%d",&r,&c);
//setbuf(stdin, NULL);
getchar();
for(int i=0;i<4*r+3;i++) {
gets(in[i]);
int len=strlen(in[i]);
for(int j=0;j<len;j++) {
if(in[i][j]=='S') {
starti=i,startj=j;
}
}
}
printf("%d\n",Search());
}
}
}