标签:des c style class blog code
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 9108 | Accepted: 3958 |
Description
Input
Output
Sample Input
2 8 8 ######## #......# #.####.# #.####.# #.####.# #.####.# #...#..# #S#E#### 9 5 ######### #.#.#.#.# S.......E #.#.#.#.# #########
Sample Output
37 5 5 17 17 9
Source
#include<stdio.h>
#include<queue>
using namespace std;
struct Node{
int x,y,s;
friend bool operator <(const Node t1,const Node t2){
return t1.s>t2.s; //按从小到大
}
};
int go[4][2]={{-1,0},{0,1},{1,0},{0,-1}}; //上右下左
int dl[4]={-1,0,1,2}; //左 前 右 后
int dr[4]={1,0,-1,-2}; //右 前 左 后
char a[45][45];
int s0,s1,s2;
int T,w,h;
void dfs1(int x,int y,int dir){ //靠左走的情况
if(a[x][y]=='E')
return ;
for(int i=0;i<4;i++){ //分别按左 前 右 后的方式走
int tmpdir=(dir+dl[i]+4)%4;
if(a[x+go[tmpdir][0]][y+go[tmpdir][1]]=='.' ||
a[x+go[tmpdir][0]][y+go[tmpdir][1]]=='E' || a[x+go[tmpdir][0]][y+go[tmpdir][1]]=='S'){
s0++;
dfs1(x+go[tmpdir][0],y+go[tmpdir][1],tmpdir);
return ;
}
}
}
void dfs2(int x,int y,int dir){ //靠右走的情况
if(a[x][y]=='E')
return ;
for(int i=0;i<4;i++){ //分别按右 前 左 后的方式走
int tmpdir=(dir+dr[i]+4)%4;
if(a[x+go[tmpdir][0]][y+go[tmpdir][1]]=='.' ||
a[x+go[tmpdir][0]][y+go[tmpdir][1]]=='E' || a[x+go[tmpdir][0]][y+go[tmpdir][1]]=='S'){
s1++;
dfs2(x+go[tmpdir][0],y+go[tmpdir][1],tmpdir);
return ;
}
}
}
int bfs(int x,int y){
int vis[45][45]; //标记数组,不要也是可以的,但会超时
for(int i=0;i<h;i++)
for(int j=0;j<w;j++)
vis[i][j]=0;
priority_queue <Node> qu; //直接用queue也是可以的,但是优先队列可以优化很多
while(!qu.empty())
qu.pop();
Node tmp; tmp.x=x; tmp.y=y; tmp.s=1;
qu.push(tmp);
while(!qu.empty()){
tmp=qu.top(); qu.pop();
if(a[tmp.x][tmp.y]=='E'){
return tmp.s;
}
if(tmp.x+1<h && !vis[tmp.x+1][tmp.y] && (a[tmp.x+1][tmp.y]=='.' || a[tmp.x+1][tmp.y]=='E')){ //向下
Node k; k.x=tmp.x+1; k.y=tmp.y; k.s=tmp.s+1;
qu.push(k);
vis[tmp.x+1][tmp.y]=1;
}
if(tmp.y+1<w && !vis[tmp.x][tmp.y+1] && (a[tmp.x][tmp.y+1]=='.' || a[tmp.x][tmp.y+1]=='E')){ //向右
Node k; k.x=tmp.x; k.y=tmp.y+1; k.s=tmp.s+1;
qu.push(k);
vis[tmp.x][tmp.y+1]=1;
}
if(tmp.x-1>=0 && !vis[tmp.x-1][tmp.y] && (a[tmp.x-1][tmp.y]=='.' || a[tmp.x-1][tmp.y]=='E')){ //向上
Node k; k.x=tmp.x-1; k.y=tmp.y; k.s=tmp.s+1;
qu.push(k);
vis[tmp.x-1][tmp.y]=1;
}
if(tmp.y-1>=0 && !vis[tmp.x][tmp.y-1] && (a[tmp.x][tmp.y-1]=='.' || a[tmp.x][tmp.y-1]=='E')){ //向左
Node k; k.x=tmp.x; k.y=tmp.y-1; k.s=tmp.s+1;
qu.push(k);
vis[tmp.x][tmp.y-1]=1;
}
}
}
int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%d",&T);
while(T--){
scanf("%d%d",&w,&h);
for(int i=0;i<h;i++)
scanf("%s",a[i]);
int flag=0,x,y,dir;
s0=s1=s2=1;
for(int i=0;i<w;i++){ //找到 S 所在位置并确定方向
if(a[0][i]=='S'){
flag=1;
x=0; y=i; dir=2;
break;
}
if(a[h-1][i]=='S'){
flag=1;
x=h-1; y=i; dir=0;
break;
}
}
if(!flag){
for(int i=0;i<h;i++){
if(a[i][0]=='S'){
x=i; y=0; dir=1;
break;
}
if(a[i][w-1]=='S'){
x=i; y=w-1; dir=3;
break;
}
}
}
dfs1(x,y,dir);
dfs2(x,y,dir);
s2=bfs(x,y);
printf("%d %d %d\n",s0,s1,s2);
}
return 0;
}
标签:des c style class blog code
原文地址:http://blog.csdn.net/my_acm/article/details/27807459