您的位置:首页 > 其它

zoj 2081 Mission Impossible——最近令我满纠结的一题--。

2010-08-26 20:02 190 查看
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1081

                                                              Mission Impossible

我做的第一道同时用DFS和BFS的题目。此题主要就是要先用BFS算出到T的最短的路径(将M忽略),然后就是用DFS来计算最短路径个数,同时将有M的最短路个标记和计数出来,相除取百分比即可。

 

终于AC了!!!

首先要感谢logiciel大大,正是由于logiciel大大检测出我的DFS的递归的步数超多,我才把注意力放在了优化函数的上面,进行了标记,递归步数一下减少到为logiciel大大提供的标准程序的递归步数的6%!


喜若狂,然后本以为可以了但是交上去还是不行,然后由于有了logiciel大大的提醒再加上我们ACM队的杨杨学长的指导,我想到了只有BFS上会有问
题,果真通过计数发现我的BFS的队列数竟然达到了3千多万,而logiciel大大给的标程BFS的队列数才62,原来罪魁祸首在这里,是我的BFS的
每次入队时没标记,导致重复走了,最终多入了如此多的点,真是不应该呀。。。怪不得会Memory Limit
Exceeded。。。标记了以后一切OK!

以下是我修改后正确的程序和logiciel大大给的标程的运行结果对比:(bfs_count 统计bfs的入队点数的 dfs_count统计递归次数的)

logiciel大大给的标程的运行结果:

1
10 10
##########
#       T#
#        #
#        #
#        #
#        #
#        #
#        #
#S       #
##########
Mission #1:
dfs_count=12034061
bfs_count=62
The probability for the spy to get to the telegraph transmitter is 100.00%.
Process returned 0 (0x0)   execution time : 2.938 s
Press any key to continue.


我的程序运行结果:

1
10 10
##########
#       T#
#        #
#        #
#        #
#        #
#        #
#        #
#S       #
##########
Mission #1:
dfs_count=780039
bfs_count=63
The probability for the spy to get to the telegraph transmitter is 100.00%.
Process returned 0 (0x0)   execution time : 1.688 s
Press any key to continue.


 

在此再次十分感谢logiciel大大的指导和所有帮助过我的人,谢谢!!!

 

最后献上我的代码,愿与大家交流:

#include<iostream>
#include<queue>
using namespace std;
struct node{
int x,y,m;
};
char map[11][11];
int mark[12][12];
int x,y;
int mines;
int shorta;
int sx,sy,ex,ey;
int road;
int num[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int dfs_count;
int bfs_count;
void BFS()
{
queue<node> qq;
node po,pi;
memset(mark,0,sizeof(mark));
int i,tx,ty,tmine;
pi.x=sx;
pi.y=sy;
pi.m=0;
mark[pi.x][pi.y]=1;
qq.push(pi);
while(!qq.empty())
{
po=qq.front();
qq.pop();
if(map[po.x][po.y]=='T')
{
shorta=po.m;
return;
}
for(i=0;i<4;i++)
{
tx=po.x+num[i][0];
ty=po.y+num[i][1];
tmine=po.m;
if(map[tx][ty]!='#' && mark[tx][ty]!=1)
{
pi.x=tx;
pi.y=ty;
pi.m=po.m+1;
mark[pi.x][pi.y]=1;
qq.push(pi);
bfs_count++;
}
}
}
return;
}
void DFS(int x,int y,int step,int mine,int x2,int y2)
{
dfs_count++;
int i,tx,ty;
if(step==shorta)
{
if(map[x][y]=='T')
{
road++;
if(mine==1)
mines++;
}
return;
}
for(i=0;i<4;i++)
{
tx=x+num[i][0];
ty=y+num[i][1];
if(map[tx][ty]!='#')
{
if(tx==x2 && ty==y2)
continue;
if(map[tx][ty]=='M' || mine==1)
DFS(tx,ty,step+1,1,x,y);
else
DFS(tx,ty,step+1,0,x,y);
}
}
return;
}
int main()
{
int mn,n;
int i,j;
mn=1;
scanf("%d",&n);

while(n--)
{

ex=-1;
ey=-1;
road=0;
shorta=-1;
mines=0;
scanf("%d %d",&x,&y);
getchar();
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='S')
{
sx=i;
sy=j;
}
if(map[i][j]=='T')
{
ex=i;
ey=j;
}
}
getchar();
}
printf("Mission #%d:/n",mn++);
if(ex==-1 && ey==-1)
{
printf("Mission Impossible./n/n");
continue;
}
bfs_count=0;
BFS();
if(shorta==-1)
{
printf("Mission Impossible./n/n");
continue;
}
dfs_count=0;
DFS(sx,sy,0,0,-1,-1);
printf("dfs_count=%d/nbfs_count=%d/n", dfs_count,bfs_count);
if(road!=0)
{
if(mines%road==0 && mines!=0)
printf("Mission Impossible./n/n");
else if(mines==0)
printf("The probability for the spy to get to the telegraph transmitter is 100.00%%./n/n");
else
printf("The probability for the spy to get to the telegraph transmitter is %.2lf%%./n/n",(((road-mines)*1.0)/road)*100);
}
else
printf("Mission Impossible./n/n");
}
return 0;
}


 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  优化 qq c