有点复杂的ZOJ 2081
先用BFS计算出所需要的最小步数,然后在BFS计算没有遇到炸弹的路径条数
计算炸弹路径稍微麻烦了一点.
这题是我写的比较痛苦的一个了,花了大概3个半小时(汗),调试时连文件输出都 用上了,唉,果然不擅长调DFS啊.
代码:
计算炸弹路径稍微麻烦了一点.
这题是我写的比较痛苦的一个了,花了大概3个半小时(汗),调试时连文件输出都 用上了,唉,果然不擅长调DFS啊.
代码:
#include <iostream>
#include <queue>
#include <cstring>/*
#include <fstream>*/
using namespace std;
struct node
{
int x,y,cnt;
};
char map[11][11];
char mapcp[11][11];
int route,cnt,mines;
node t,tt,start,end;
int boom;/*
fstream outfile;*/
void chuandfs(int x,int y,int l)
{
int a,b;
if(l==cnt)
{
/*outfile << "*** x: " << x << " ";
outfile << "y: " << y << " ";
outfile << "l: " << l << endl;*/
if(x==end.x&&y==end.y)
{
if(boom>0)
{
mines++;
}
route++;
}
return;
}
/*outfile << endl;
outfile << "x: " << x << " ";
outfile << "y: " << y << " ";
outfile << "boom: " << boom << " ";
outfile << "mines: " << mines << " ";
outfile << "route: " << route << " ";
outfile << "l: " << l << endl;*/
/*for(int i=0;i<6;++i)
{
for(int j=0;j<10;++j)
outfile << mapcp[i][j];
outfile << endl;
}*/
a = x+1;b = y;
if(mapcp[a][b]!='#')
{
if(mapcp[a][b]=='M')
boom++;
mapcp[a][b] = '#';
chuandfs(a,b,l+1);
mapcp[a][b] = map[a][b];
if(mapcp[a][b]=='M')
boom--;
}
a = x-1;b = y;
if(mapcp[a][b]!='#')
{
if(mapcp[a][b]=='M')
boom++;
mapcp[a][b] = '#';
chuandfs(a,b,l+1);
mapcp[a][b] = map[a][b];
if(mapcp[a][b]=='M')
boom--;
}
a = x;b = y+1;
if(mapcp[a][b]!='#')
{
if(mapcp[a][b]=='M')
boom++;
mapcp[a][b] = '#';
chuandfs(a,b,l+1);
mapcp[a][b] = map[a][b];
if(mapcp[a][b]=='M')
boom--;
}
a = x;b = y-1;
if(mapcp[a][b]!='#')
{
if(mapcp[a][b]=='M')
boom++;
mapcp[a][b] = '#';
chuandfs(a,b,l+1);
mapcp[a][b] = map[a][b];
if(mapcp[a][b]=='M')
boom--;
}
}
int main()
{
/*
outfile.open("out");*/
int x;char ct;
int m,n,i,j,ccc=1;bool ff,path;
queue<node> rer;
cin >> x;
while(x--)
{
cin >> n >> m;
getchar();
for(i=0;i<n;++i)
{
for(j=0;j<m;++j)
{
ct = getchar();
if(ct=='\n')
ct = getchar();
map[i][j] = ct;
if(ct=='S')
{
start.x = i;
start.y = j;
start.cnt = 0;
}
else if(ct=='T')
{
end.x = i;
end.y = j;
}
}
}
for(i=0;i<n;++i)
for(j=0;j<m;++j)
mapcp[i][j] = map[i][j];
// bfs
mapcp[start.x][start.y] = '#';
rer.push(start);ff = false;
while(!rer.empty())
{
t = rer.front();
rer.pop();
if(t.x==end.x&&t.y==end.y)
{
ff = true;
break;
}
tt = t;
if(map[tt.x+1][tt.y]!='#')
{
tt.x++;
tt.cnt++;
rer.push(tt);
map[tt.x][tt.y] = '#';
}
tt = t;
if(map[tt.x-1][tt.y]!='#')
{
tt.x--;
tt.cnt++;
rer.push(tt);
map[tt.x][tt.y] = '#';
}
tt = t;
if(map[tt.x][tt.y+1]!='#')
{
tt.y++;
tt.cnt++;
rer.push(tt);
map[tt.x][tt.y] = '#';
}
tt = t;
if(map[tt.x][tt.y-1]!='#')
{
tt.y--;
tt.cnt++;
rer.push(tt);
map[tt.x][tt.y] = '#';
}
}
while(!rer.empty())
rer.pop();
if(ff==false)
{
cout << "Mission #" << ccc++ << ":" << endl;
cout << "Mission Impossible." << endl << endl;
continue;
}
for(i=0;i<n;++i)
for(j=0;j<m;++j)
map[i][j] = mapcp[i][j];
cnt = t.cnt;
route = mines = 0;boom = 0;
mapcp[start.x][start.y] = '#';
chuandfs(start.x,start.y,0);
cout << "Mission #" << ccc++ << ":" << endl;
if(mines==route)
{
cout << "Mission Impossible." << endl << endl;
}
else
{
double result = ((double)(route-mines))/route*100;
printf("The probability for the spy to get to the telegraph transmitter is %.2lf",result);
cout << "%.\n\n";
}
/*outfile.close();*/
}
return 0;
}
#include <queue>
#include <cstring>/*
#include <fstream>*/
using namespace std;
struct node
{
int x,y,cnt;
};
char map[11][11];
char mapcp[11][11];
int route,cnt,mines;
node t,tt,start,end;
int boom;/*
fstream outfile;*/
void chuandfs(int x,int y,int l)
{
int a,b;
if(l==cnt)
{
/*outfile << "*** x: " << x << " ";
outfile << "y: " << y << " ";
outfile << "l: " << l << endl;*/
if(x==end.x&&y==end.y)
{
if(boom>0)
{
mines++;
}
route++;
}
return;
}
/*outfile << endl;
outfile << "x: " << x << " ";
outfile << "y: " << y << " ";
outfile << "boom: " << boom << " ";
outfile << "mines: " << mines << " ";
outfile << "route: " << route << " ";
outfile << "l: " << l << endl;*/
/*for(int i=0;i<6;++i)
{
for(int j=0;j<10;++j)
outfile << mapcp[i][j];
outfile << endl;
}*/
a = x+1;b = y;
if(mapcp[a][b]!='#')
{
if(mapcp[a][b]=='M')
boom++;
mapcp[a][b] = '#';
chuandfs(a,b,l+1);
mapcp[a][b] = map[a][b];
if(mapcp[a][b]=='M')
boom--;
}
a = x-1;b = y;
if(mapcp[a][b]!='#')
{
if(mapcp[a][b]=='M')
boom++;
mapcp[a][b] = '#';
chuandfs(a,b,l+1);
mapcp[a][b] = map[a][b];
if(mapcp[a][b]=='M')
boom--;
}
a = x;b = y+1;
if(mapcp[a][b]!='#')
{
if(mapcp[a][b]=='M')
boom++;
mapcp[a][b] = '#';
chuandfs(a,b,l+1);
mapcp[a][b] = map[a][b];
if(mapcp[a][b]=='M')
boom--;
}
a = x;b = y-1;
if(mapcp[a][b]!='#')
{
if(mapcp[a][b]=='M')
boom++;
mapcp[a][b] = '#';
chuandfs(a,b,l+1);
mapcp[a][b] = map[a][b];
if(mapcp[a][b]=='M')
boom--;
}
}
int main()
{
/*
outfile.open("out");*/
int x;char ct;
int m,n,i,j,ccc=1;bool ff,path;
queue<node> rer;
cin >> x;
while(x--)
{
cin >> n >> m;
getchar();
for(i=0;i<n;++i)
{
for(j=0;j<m;++j)
{
ct = getchar();
if(ct=='\n')
ct = getchar();
map[i][j] = ct;
if(ct=='S')
{
start.x = i;
start.y = j;
start.cnt = 0;
}
else if(ct=='T')
{
end.x = i;
end.y = j;
}
}
}
for(i=0;i<n;++i)
for(j=0;j<m;++j)
mapcp[i][j] = map[i][j];
// bfs
mapcp[start.x][start.y] = '#';
rer.push(start);ff = false;
while(!rer.empty())
{
t = rer.front();
rer.pop();
if(t.x==end.x&&t.y==end.y)
{
ff = true;
break;
}
tt = t;
if(map[tt.x+1][tt.y]!='#')
{
tt.x++;
tt.cnt++;
rer.push(tt);
map[tt.x][tt.y] = '#';
}
tt = t;
if(map[tt.x-1][tt.y]!='#')
{
tt.x--;
tt.cnt++;
rer.push(tt);
map[tt.x][tt.y] = '#';
}
tt = t;
if(map[tt.x][tt.y+1]!='#')
{
tt.y++;
tt.cnt++;
rer.push(tt);
map[tt.x][tt.y] = '#';
}
tt = t;
if(map[tt.x][tt.y-1]!='#')
{
tt.y--;
tt.cnt++;
rer.push(tt);
map[tt.x][tt.y] = '#';
}
}
while(!rer.empty())
rer.pop();
if(ff==false)
{
cout << "Mission #" << ccc++ << ":" << endl;
cout << "Mission Impossible." << endl << endl;
continue;
}
for(i=0;i<n;++i)
for(j=0;j<m;++j)
map[i][j] = mapcp[i][j];
cnt = t.cnt;
route = mines = 0;boom = 0;
mapcp[start.x][start.y] = '#';
chuandfs(start.x,start.y,0);
cout << "Mission #" << ccc++ << ":" << endl;
if(mines==route)
{
cout << "Mission Impossible." << endl << endl;
}
else
{
double result = ((double)(route-mines))/route*100;
printf("The probability for the spy to get to the telegraph transmitter is %.2lf",result);
cout << "%.\n\n";
}
/*outfile.close();*/
}
return 0;
}
浙公网安备 33010602011771号