您的位置:首页 > 其它

nyoj-----284坦克大战(带权值的图搜索)

2014-06-11 06:34 218 查看

坦克大战

时间限制:1000 ms | 内存限制:65535 KB
难度:3

描述
Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now.
What we are discussing is a simple edition of this game. Given a map that consists of empty spaces, rivers, steel walls and brick walls only. Your task is to get a bonus as soon as possible suppose that no enemies will disturb you (See the following picture).

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cstdlib>
using namespace std;
const int maxn=302;
struct node
{
int val;
int minc;
};
node map[maxn][maxn];
//先采用深度搜索
int dir[4][2]=
{
{0,1},    //向右
{-1,0},   //向左
{1,0},    //向上
{0,-1}    //向下
};
int m,n,sx,sy,ex,ey;
void dfs(int x,int y)
{
//剪枝
if(x>0&&y>0&&x<=m&&y<=n)
{
for(int i=0;i<4;i++)
{
if(map[x+dir[i][0]][y+dir[i][1]].val>0&&map[x+dir[i][0]][y+dir[i][1]].minc>(map[x][y].minc+map[x+dir[i][0]][y+dir[i][1]].val))  //非智能 shit
{
map[x+dir[i][0]][y+dir[i][1]].minc=(map[x][y].minc+map[x+dir[i][0]][y+dir[i][1]].val);
dfs(x+dir[i][0],y+dir[i][1]);
}
}
}
}

int main()
{
int i,j;
char ss;
// freopen("test.in","r",stdin);
while(scanf("%d%d",&m,&n)!=EOF&&n+m)
{
getchar();
memset(map,0,sizeof(map));
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
map[i][j].minc=0x3f3f3f3f;
scanf("%c",&ss);
if(ss=='E')
map[i][j].val=1;
else if(ss=='B')
map[i][j].val=2;
else if(ss=='Y')
{
map[i][j].minc=0;
sx=i;
sy=j;
}
else if(ss=='T')
{
map[i][j].val=1;
ex=i;
ey=j;
}
else
map[i][j].val=-1; //代表障碍物
}
getchar();
}
//algorithm functiom
dfs(sx,sy);
if(map[ex][ey].minc==0x3f3f3f3f)
printf("-1\n");
else
printf("%d\n",map[ex][ey].minc);
}
return 0;
}


View Code
当然还可以用bfs来做,时间会更快什么的........
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: