TOJ 3944 Maze, amazing / 广搜

来源:互联网 发布:网络电视怎样看电视 编辑:程序博客网 时间:2024/05/16 14:27

Maze, amazing

时间限制(普通/Java):1000MS/3000MS     运行内存限制:65536KByte
 

描述

Let’s play maze game again! Maze problem is an old problem but here comes a challenge one. Max, an intelligent robot, is trapped in an N*M maze grid. He is located at a start position initially and aiming to find the exit of the maze. In the maze there are numbers of pillars which are set as obstacles. Max is energetic and not afraid of strolling in the maze at all. But he dislikes turning round in the maze all through. “Turning left or right several times keeps me uncomfortable and confused! It makes me feel sick and unconfident to run out of this maze!” Max said.
Given the cost of turning left and right of Max, the description of the maze grid, the start position and destination, you are going to give your hand to Max by calculating the minimum cost of turning round (no matter left or right) to get to the exit from start position.
Note: Max can just perform three operations at one time: go front, turn left, and turn right. Initially Max stands at the start point and you should decide in which direction he starts the first step in order to minimize the total dislike of Max.

输入

Input may consist of several test data sets. For each data set, it can be format as below:
First comes two integers in one line separating with one space: l(1 <= l <= 100) representing the cost of turning left of Max, r(1 <= r <= 100) representing the cost of turning right of Max.
Then six integers follows in next coming line separating with one space: r1 (1 <= r1 <= 100) representing the number of rows of the maze grid, c (1 <= c <= 100) representing the number of columns of the maze grid, sx (1 <= sx <= r1) representing the row position of the start position of Max, sy (1 <= sy <= c) representing the column position of the start position of Max, ex (1 <= ex <= r1) representing the row position of the exit of the maze, ey (1 <= ey <= c) representing the column position of the exit of the maze.
Finally comes rl row(s) with c character(s) in each row. This part represents the maze data part and all character(s) can only be two types: ‘*’ representing a pillar of the maze and ‘.’ representing an empty grid cell that Max can stand on. Position of cell in the upperleft corner of the grid is (1, 1).
Input is ended with l = r = 0.
Note: Max can’t go outside the range of the row and column of the maze, either go pass the pillar. You can assume the input is legal that means there is no pillar in the start position and exit of the maze.

输出

Output one integer in one line representing the minimum cost of turning to get to the exit if there is one way to get there, or output -1 in one line if it is impossible for Max to get to the destination.

样例输入

1 21 4 1 1 1 4....1 2 1 4 1 1 1 4..*.1 22 4 2 1 2 4......*.1 23 4 2 1 2 4*.....*.*...

用了优先队列 开了个3维数组表示到某一点的4个方向过来的 还是错了

后来改了一下 不是第一次到的是最小的不能直接标记成走过的  改了一下就对了

#include <stdio.h>#include <queue>#include <string.h>#include <string>#include <algorithm>using namespace std;char a[110][110];int map[110][110][4];struct node{int x;int y;int step;int dire;bool friend operator <(node a,node b){return a.step > b.step;}};node s,e;int l,r,n,m;int dir[4][2] = {1,0,-1,0,0,1,0,-1};//下上右左 int flag;int minn;int bfs(){priority_queue <node> q;s.step = 0;q.push(s);int i = 0;while(!q.empty()){node p = q.top();q.pop();if(p.x == e.x && p.y == e.y){flag = 1;if(minn > p.step)minn = p.step;continue;}for(i = 0;i < 4; i++){if(p.dire != -1 && (p.dire == 0 && i == 1 || p.dire == 1 && i == 0 || p.dire == 2 && i == 3 || p.dire == 3 && i == 2))continue;node t;t.x = p.x + dir[i][0];t.y = p.y + dir[i][1];t.dire = i;if(t.x >= 0 && t.x < n && t.y >= 0 && t.y < m && a[t.x][t.y] != '*'){if(p.dire == -1 || p.dire == t.dire)t.step = p.step;else{if(p.dire == 0 && t.dire == 2 || p.dire == 1 && t.dire == 3 || p.dire == 2 && t.dire == 1 || p.dire == 3 && t.dire == 0)t.step = p.step + l;else if(p.dire == 0 && t.dire == 3 || p.dire == 1 && t.dire == 2 || p.dire == 2 && t.dire == 0 || p.dire == 3 && t.dire == 1)t.step = p.step + r;} //printf("%d %d %d %d\n",t.x,t.y,t.step,t.dire);if(map[t.x][t.y][t.dire] == -1 || map[t.x][t.y][t.dire] > t.step){q.push(t);map[t.x][t.y][t.dire] = t.step;}}}}if(!flag)return -1;elsereturn minn;}int main(){int i,j;while(scanf("%d %d",&l,&r),l||r){minn = 0x7fffffff;flag = 0;scanf("%d %d",&n,&m);scanf("%d %d %d %d",&s.x,&s.y,&e.x,&e.y);for(i = 0;i < n; i++)scanf("%s",a[i]);s.x--;s.y--;e.x--;e.y--;s.dire = -1;memset(map,-1,sizeof(map));map[s.x][s.y][0] = 0;map[s.x][s.y][1] = 0;map[s.x][s.y][2] = 0;map[s.x][s.y][3] = 0;printf("%d\n",bfs());}return 0;}


 

原创粉丝点击