HDU
来源:互联网 发布:淘宝信用贷款和网商贷 编辑:程序博客网 时间:2024/06/08 10:57
Tempter of the Bone II
Problem Description
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze was changed and the way he came in was lost.He realized that the bone was a trap, and he tried desperately to get out of this maze.
The maze was a rectangle with the sizes of N by M. The maze is made up of a door,many walls and many explosives. Doggie need to reach the door to escape from the tempter. In every second, he could move one block to one of the upper, lower, left or right neighboring blocks. And if the destination is a wall, Doggie need another second explode and a explosive to explode it if he had some explosives. Once he entered a block with explosives,he can take away all of the explosives. Can the poor doggie survive? Please help him.
The maze was a rectangle with the sizes of N by M. The maze is made up of a door,many walls and many explosives. Doggie need to reach the door to escape from the tempter. In every second, he could move one block to one of the upper, lower, left or right neighboring blocks. And if the destination is a wall, Doggie need another second explode and a explosive to explode it if he had some explosives. Once he entered a block with explosives,he can take away all of the explosives. Can the poor doggie survive? Please help him.
Input
The input consists of multiple test cases. The first line of each test case contains two integers N, M,(2 <= N, M <= 8). which denote the sizes of the maze.The next N lines give the maze layout, with each line containing M characters. A character is one of the following:
'X': a block of wall;
'S': the start point of the doggie;
'D': the Door;
'.': an empty block;
'1'--'9':explosives in that block.
Note,initially he had no explosives.
The input is terminated with two 0's. This test case is not to be processed.
'X': a block of wall;
'S': the start point of the doggie;
'D': the Door;
'.': an empty block;
'1'--'9':explosives in that block.
Note,initially he had no explosives.
The input is terminated with two 0's. This test case is not to be processed.
Output
For each test case, print the minimum time the doggie need to escape if the doggie can survive, or -1 otherwise.
Sample Input
4 4SX..XX......1..D4 4S.X1......XX..XD0 0
Sample Output
-19
解题思路:状态压缩BFS,标记一下炸弹
#include<iostream>#include<queue>#include<memory.h>#include<stdio.h>#include<map>#include<string>#include<algorithm>#include<vector>using namespace std;typedef unsigned long long ll;int N,M;char maze[10][10];int wallid[10][10];int bomeid[10][10];vector<ll> vis[10][10][9*8*8];vector<ll> pic[10][10][9*8*8];bool visited(int i,int j,int s,ll cur){ for(int k=0;k<vis[i][j][s].size();k++){ if(cur==vis[i][j][s][k]) return 1; } return 0;}bool picked(int i,int j,int s,ll cur){ for(int k=0;k<pic[i][j][s].size();k++){ if(cur==pic[i][j][s][k]) return 1; } return 0;}struct point{ int x; int y; int time; int bome; ll state; ll spick; point(int a,int b,int c,int d,ll s,ll p){ x=a; y=b; time=c; bome=d; state=s; spick=p; } bool operator<(const point n1) const{ return time>n1.time; } point(){}};int dx[4]={0,-1,0,1};int dy[4]={1,0,-1,0};int ans=9999999;void bfs(point start,point end){ int aaa=0; priority_queue<point> que; que.push(start); vis[start.x][start.y][start.bome].push_back(start.state); pic[start.x][start.y][start.bome].push_back(start.spick); while(!que.empty()){ point tp=que.top(); que.pop(); if(tp.x==end.x&&tp.y==end.y){ aaa++; ans=min(ans,tp.time); if(aaa>1) return; } for(int i=0;i<4;i++){ int x=tp.x+dx[i]; int y=tp.y+dy[i]; int t=tp.time; int b=tp.bome; ll s=tp.state; ll p=tp.spick; int vb=visited(x,y,b,s); int pb=picked(x,y,b,p); if(!vb||!pb){ if(!vb) vis[x][y][b].push_back(s); if(!pb) pic[x][y][b].push_back(p); if(maze[x][y]=='.'){ que.push(point(x,y,t+1,b,s,p)); } else{ if(maze[x][y]=='X'){ if(!(s&(1LL<<wallid[x][y]))){ if(b>0){ b--; int id=wallid[x][y]; s=s|(1LL<<id); vis[x][y][b].push_back(s); que.push(point(x,y,t+2,b,s,p)); } } else{ que.push(point(x,y,t+1,b,s,p)); } } else{ if(maze[x][y]!='#'){ if(!(p&(1LL<<bomeid[x][y]))){ int num=maze[x][y]-'0'; b+=num; int id=bomeid[x][y]; p=p|(1LL<<id); pic[x][y][b].push_back(p); que.push(point(x,y,t+1,b,s,p)); } else{ que.push(point(x,y,t+1,b,s,p)); } } } } } } }}int main(){ while(1){ scanf("%d%d",&N,&M); if(N==0&&M==0) break; for(int i=0;i<10;i++) for(int j=0;j<10;j++){ maze[i][j]='#'; } for(int i=0;i<10;i++) for(int j=0;j<10;j++) for(int k=0;k<9*8*8;k++){ vis[i][j][k].clear(); pic[i][j][k].clear(); } point s,e; int wid=0; int bid=0; for(int i=1;i<=N;i++) for(int j=1;j<=M;j++){ cin>>maze[i][j]; if(maze[i][j]=='S'){ s=point(i,j,0,0,0,0); maze[i][j]='.'; continue; } if(maze[i][j]=='D'){ e=point(i,j,0,0,0,0); maze[i][j]='.'; continue; } if(maze[i][j]=='X'){ wallid[i][j]=wid++; continue; } if(maze[i][j]!='.'){ bomeid[i][j]=bid++; } } ans=9999999; bfs(s,e); if(ans!=9999999) printf("%d\n",ans); else printf("%d\n",-1); } return 0;}
阅读全文
0 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- 在已绑定数据源的combobox中增加一行
- [leetcode]: 58. Length of Last Word
- 链队列和循环队列的表示与实现-初始化、判空、求长、入队、出队、遍历、求头、求尾、清队、毁队
- Centos 7 和 Centos 6 的区别
- cocos2dx坐标系
- HDU
- C++ 中 public、protected 及 private 的作用总结
- C语言封装成object-c的过程,实现原理
- unity shader入门精要 前3章总结
- Native与html5交互
- 《设计心理学》
- Spring--MVC--如何书写MVC的控制层Controller
- 虚拟机下Linux系统的安装及项目部署详解
- Centos 7 防火墙