拯救我的女神

来源:互联网 发布:java lambda select 编辑:程序博客网 时间:2024/04/29 18:44
描述

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.

输入
The input consists of blocks of lines. There is a blank line between two blocks.

The first line of each block contains two positive integers M <= 50 and N <= 50separated by one space. In each of the next M lines there is a string of length N contentsO and #.

O represents empty squares. # means a wall.

At last, the location of Miss Ice, ( x, y ). 1 <= x <= M, 1 <= y <= N.

(Shining Knight always starts at coordinate ( 1, 1 ). Both Shining and Ice's locationguarantee not to be a wall.)
输出
The least amount of time Shining Knight takes to save hisgoddess in one line.
样例输入
3 5O##########O#O#3 4

样例输出

14

题目理解:

输入有三部分  第一行 有两个数字分别代表下面几行(除了最后一行)的行数和列数 最后一行代表的是  公主的位置(坐标) 中间的几行有"#"和"O"他们分别代表的意思是    "#"中间是一个屋子四面都是墙 "O"的意思是
四面都是空的(没有墙) 王子手里的宝剑 在三个单位时间内 可以 打破一堵墙 王子从一个房间 跨越到另一个房间需要 1个单位时间 (王子只能 在上下左右 这四个方向上移动 ) 现在给你 上述信息 你需要求出来王子 在用时 最短的情况下 几分钟可以将 公主解救出来 .


相关链接https://baike.so.com/doc/4988346-5211910.html

C语言AC代码:


时间内存语言结果32324CAccepted

#include <stdio.h>#include <string.h>int min=1000000,a,b,visit[55][55];char maze[55][55];void dfs(int x,int y,int m,int n,int sum){if(x==m&&y==n){if(sum<min)min=sum;}else{if(x-1>0&&(!visit[x-1][y])){visit[x-1][y]=1;if(maze[x-1][y]=='#') dfs(x-1,y,m,n,sum+4);else dfs(x-1,y,m,n,sum+1);visit[x-1][y]=0;}if(x+1<=a&&(!visit[x+1][y])){visit[x+1][y]=1;if(maze[x+1][y]=='#') dfs(x+1,y,m,n,sum+4);else dfs(x+1,y,m,n,sum+1);visit[x+1][y]=0;}if(y-1>0&&(!visit[x][y-1])){visit[x][y-1]=1;if(maze[x][y-1]=='#') dfs(x,y-1,m,n,sum+4);else dfs(x,y-1,m,n,sum+1);visit[x][y-1]=0;}if(y+1<=b&&(!visit[x][y+1])){visit[x][y+1]=1;if(maze[x][y+1]=='#') dfs(x,y+1,m,n,sum+4);else dfs(x,y+1,m,n,sum+1);visit[x][y+1]=0;}}}int main(){int i,j,m,n;while(scanf("%d%d%*c",&a,&b)==2)//等价于while(scanf(%d%d%*c",&a,&b)!=EOF){for(i=1;i<=a;i++){for(j=1;j<=b;j++)scanf("%c",&maze[i][j]);getchar();}scanf("%d%d",&m,&n);memset(visit,0,sizeof(visit));visit[1][1]=1;dfs(1,1,m,n,0);printf("%d\n",min);}return 0;}

原创粉丝点击