Shining Knight is the embodiment of justice and he has a very sharp sword can even cleavewall. Many bad guys are dead on his sword.
One day, two evil sorcerer cgangee and Jackchess decided to give him some colorto see. So they kidnapped Shining Knight's beloved girl--Miss Ice! They built a M x Nmaze with magic and shut her up in it.
Shining Knight arrives at the maze entrance immediately. He can reach any adjacent emptysquare of four directions -- up, down, left, and right in 1 second. Or cleave one adjacent wall in 3
seconds, namely,turn it into empty square. It's the time to save his goddess! Notice: ShiningKnight won't leave the maze before he find Miss Ice.
3 5 O#### ##### #O#O# 3 4
14
一个0和O的闹剧,把O看成了0,我也是醉了(衰
ac代码:
#include<stdio.h> #include<string.h> #include<iostream> #include<queue> #include<algorithm> using namespace std; int n,m; int ex,ey; char map[55][55]; int hp[55][55]; int v[55][55]; int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; struct s { int x; int y; int step; friend bool operator<(s n1,s n2) { return n2.step<n1.step; } }; int check(int xx,int yy) { if(xx<=0||yy<0||xx>n||yy>=m) return 1; if(v[xx][yy]) return 1; return 0; } int bfs() { int i; priority_queue<s>q; s a,b; a.x=1; a.y=0; a.step=0; v[a.x][a.y]=1; q.push(a); while(!q.empty()) { a=q.top(); q.pop(); if(a.x==ex&&a.y==ey-1) { return a.step; } for(i=0;i<4;i++) { int nx=a.x+dir[i][0]; int ny=a.y+dir[i][1]; if(check(nx,ny)==0) { b.step=a.step+hp[nx][ny]+1; b.x=nx;b.y=ny; v[b.x][b.y]=1; q.push(b); } } } return 0; } int main() { int i,j; int sum; while(scanf("%d%d",&n,&m)!=EOF) { memset(hp,0,sizeof(hp)); memset(v,0,sizeof(v)); for(i=1;i<=n;i++) { scanf("%s",map[i]); for(j=0;j<m;j++) { if(map[i][j]=='O') hp[i][j]=0; else if(map[i][j]=='#') hp[i][j]=3; } } scanf("%d%d",&ex,&ey); sum=bfs(); printf("%d\n",sum); } return 0; }