POJ 3346 && HDU 2416 Treasure of the Chimp Island(bfs)
来源:互联网 发布:淘宝营销文案 编辑:程序博客网 时间:2024/06/05 16:24
Description
一个n*m的地图,.可以走,*不能走,起点在四周,每一个起点有一定数量的炸弹,#表示该起点没有炸弹,起点是一个字母则表示该起点有与该字母在字母表中位置相同数量的炸弹,在地图中数字表示该处有墙,有两种方案走过该点,一是多花费和该数字一样的时间去把墙挖倒,二是用一个炸弹瞬间炸穿墙,¥表示该处有钱,问从起点走到¥处的最短时间
Input
多组用例,每组用例输入一个字符矩阵表示该地图,相邻两组用例用空行隔开,以”–”结束输入,矩阵行列数不超过100
Output
输出起点集合到终点的最短用时,如果不可达则输出“IMPOSSIBLE”
Sample Input
Sample Output
1
IMPOSSIBLE
Solution
把一个点拆成27个点分别记录到该点还剩i个炸弹的最短用时(0<=i<=26),然后bfs即可,具体操作看代码
Code
#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#include<cmath>#include<vector>#include<queue>#include<map>#include<set>#include<ctime>using namespace std;typedef long long ll;#define INF 0x3f3f3f3f#define maxn 111char s[maxn][maxn];struct node{ int x,y,num;};int n,m,ex,ey,t[maxn][maxn][27],vis[maxn][maxn][27];int dx[]={-1,0,1,0};int dy[]={0,-1,0,1};void bfs(){ queue<node>que; node now,temp; for(int i=0;i<n;i++) for(int j=0;j<m;j++) { for(int k=0;k<27;k++) t[i][j][k]=INF,vis[i][j][k]=0; if(s[i][j]=='$')ex=i,ey=j; else if(s[i][j]=='#'||s[i][j]>='A'&&s[i][j]<='Z') { now.x=i,now.y=j; if(s[i][j]=='#')now.num=0; else now.num=s[i][j]-'A'+1; s[i][j]='*',t[i][j][now.num]=0,vis[i][j][now.num]=1; que.push(now); } } while(!que.empty()) { now=que.front(); que.pop(); int x=now.x,y=now.y,num=now.num; vis[x][y][num]=0; for(int i=0;i<4;i++) { int xx=x+dx[i],yy=y+dy[i]; temp.x=xx,temp.y=yy,temp.num=num; if(xx<0||xx>=n||yy<0||yy>=m||s[xx][yy]=='*')continue; if((s[xx][yy]=='.'||s[xx][yy]=='$')&&t[xx][yy][num]>t[x][y][num]) { t[xx][yy][num]=t[x][y][num]; if(!vis[xx][yy][num])vis[xx][yy][num]=1,que.push(temp); } else if(s[xx][yy]>='1'&&s[xx][yy]<='9') { int cnt=s[xx][yy]-'0'; if(t[xx][yy][num]>t[x][y][num]+cnt) { t[xx][yy][num]=t[x][y][num]+cnt; if(!vis[xx][yy][num])vis[xx][yy][num]=1,que.push(temp); } if(num&&t[xx][yy][num-1]>t[x][y][num]) { t[xx][yy][num-1]=t[x][y][num]; temp.num--; if(!vis[xx][yy][num-1])vis[xx][yy][num-1]=1,que.push(temp); } } } } int ans=INF; for(int i=0;i<27;i++)ans=min(ans,t[ex][ey][i]); if(ans==INF)printf("IMPOSSIBLE\n"); else printf("%d\n",ans); }int main(){ while(gets(s[0]),s[0][0]!='-') { n=1; while(gets(s[n++])) { if(s[n-1][0]=='\0') { n--; break; } } m=strlen(s[0]); bfs(); } return 0;}
阅读全文
0 0
- POJ 3346 && HDU 2416 Treasure of the Chimp Island(bfs)
- HDU 2416 Treasure of the Chimp Island (BFS)
- Treasure of the Chimp Island hdu bfs
- HDU 2416 Treasure of the Chimp Island bfs 最短路
- HDU 2416 POJ 3346 Treasure of the Chimp Island(最短路)
- HDU 2416 Treasure of the Chimp Island
- hdu 2416 Treasure of the Chimp Island(bfs求最短路)
- hdu(2416) Treasure of the Chimp Island
- Treasure of the Chimp Island (BFS
- HDOJ 2416 Treasure of the Chimp Island (BFS)
- HDU2416 Treasure of the Chimp Island ,bfs+优先队列
- POJ 3346 Treasure of the Chimp Island 和CCF考试题比较像
- POJ 3346 Treasure of theChimp Island(BFS)
- (二分,BFS).Treasure Island
- poj 1924 The Treasure (bfs+地图预处理)
- POJ1924----The Treasure----BFS
- POJ 1924 The Treasure
- poj Children of the Candy Corn(Bfs + Dfs)
- Shadowsocks部署和共享
- 【HDU-4004 】The Frog's Games 【二分+思维】
- javscript-json和xml简单了解
- in 和 exist 区别
- stringbuilder,streamwriter写xml
- POJ 3346 && HDU 2416 Treasure of the Chimp Island(bfs)
- 单词方阵解题报告
- 【poj 3061】Subsequence 【尺取法】or【前缀和+二分】
- leetcode 51. N-Queens
- 机器人走方格I
- C++文件读写详解(ofstream,ifstream,fstream)
- A Sweet Journey 【二分】
- 访问者模式
- windows下在命令行模式下使用MinGW编译C代码,出现cannot find ipthread