Wajueji which home strong! Time limit: +Ms | Memory Limit:65535KB Difficulty: 2
-
-
Describe
-
in a ravine, the two siblings also admitted to the university. But it's not good news because the family is in a pinch. Fathersay to the child:I'm zaguomaitie, and I'm going to give you both your sisters. Was's sisterhave decided to give up the chance to go to school. Did not expect the second day is not bright, the younger brother secretly with a few broken clothes and a few dry baba steamed bread, inSisterThe pillow left a note: SIS,You do not worry, it is not easy to enter the university, I go out to work for you. Brother.Sisterholding that note,Lying on the Kang, cried bitterly. That year, the younger brother was 17 years old,Sister20 years old. SisterThe money he borrowed from his father's village and his brother's money to move cement at the site ended up reading a junior. DaySisteris in the dorminReading,The students ran in.said to the elder sister,A fellow is looking for you. sister is very puzzled,Get out.after theSee my brother from afar,Wearing overalls covered with cement and sand.Sister said, how do you?and my classmates said you are my fellow ah?He smiled and said, you see I wear this, say it is your brother, your classmates still not laugh at you?Sisterthe nose is sour, the tears fall down. Brotherquickly forSisterwipe the tears, said: elder sister, you do not cry, I come here is to let you help me inquire about, learn excavator which strong?
With your help, the younger brother set foot on the road to Blue Cheung.
Then the problem comes.
-
Input
-
The first number t,t the set of test data.
Two number n, m; (0< N, M <= 100) represents a two-dimensional map of an H row M column.
The next n lines are m characters per line.
' s ' indicates where his brother is currently located.
' # ' means a mountain here. In order to save energy, do not pass from here.
From the ' A '-' Z ' represents the economic level around, corresponding to 1-26, passing the corresponding character of the region needs to pay the corresponding living expenses.
' L ' indicates the location of the Lanxiang Technical School.
Both S and L are lowercase letters.
The younger brother can only walk in four directions.
-
Output
-
The output of a number indicates that the younger brother to Blue Cheung needs to the minimum cost of living.
If not reachable, output-1.
-
Sample input
-
5#svgfa# #ZAlCDBC3 3sababsabl3 3s#b## #ABl
-
Sample output
-
484-1
-
Source
-
Unease
-
Uploaded by
-
acm_ Ann Pengcheng
-
-
-
Ideas:
-
-
-
-
This problem is a naked wide search, directly with the knowledge of the wide search, and then the corresponding number of steps to change the corresponding number is OK! See the code specifically:
-
-
-
-
Code:
-
-
-
#include <stdio.h> #include <string.h> #include <math.h> #include <queue> #include < Algorithm> #define INF 0xfffffffusing namespace Std;int n,m,ans;char map[105][105];int x,y,ex,ey;int vis[105][105]; int fx[4]={0,1,-1,0};int fy[4]={1,0,0,-1};struct Node {int x,y,step;friend bool operator < (node A,node b) {return a.ste P>b.step;}} A,temp;int judge () {if (temp.x<1| | Temp.x>n) return 0;if (temp.y<1| | TEMP.Y>M) return 0;if (map[temp.x][temp.y]== ' # ') return 0;if (vis[temp.x][temp.y]==1) return 0;if (Temp.step>=ans ) return 0;return 1;} void BFs () {A.x=x;a.y=y;a.step=0;memset (vis,0,sizeof (Vis)); Vis[x][y]=1;priority_queue<node>q;q.push (a); while (!q.empty ()) {a=q.top (), Q.pop (); for (int i=0;i<4;i++) {temp.x=a.x+fx[i];temp.y=a.y+fy[i];temp.step=a.step+ map[temp.x][temp.y]-' A ' +1;if (judge ()) {if (Temp.x==ex&&temp.y==ey) {ans=temp.step-(' l '-' a ' + 1); return;} Vis[temp.x][temp.y]=1;q.push (temp);}}} int main () {int t;scanf ("%d", &t), while (t--) {scanf ("%d%d", &n, &m) ans=inf;for (int i=1;i<=n;i++) {getchar (); for (int j=1;j<=m;j++) {scanf ("%c", &map[i][j]); if (map[i][j]== ' s ') {x=i;y=j; } else if (map[i][j]== ' l ') {ex=i;ey=j; }}}BFS (), if (ans!=inf) printf ("%d\n", ans); elseprintf (" -1\n");} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
NY 1100 Wajueji which home strong!