Nyoj 1100 Wajueji which home strong!

Source: Internet
Author: User

Wajueji which home strong! Time limit:MS | Memory limit:65535 KB 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. The father said to the child: "I am zaguomaitie also want to give your sister both out." The sister had 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, in the sister left a note: sister, you do not worry, admitted to the university is not easy, I went out to work for you. Brother. Sister holding the note, lying on the Kang, and cried bitterly. That year, the younger brother 17 years old, elder sister 20 years old. My sister spent the money he borrowed from his father's village and his brother's money to move cement at the construction site and finally read about the juniors. One day my sister is reading in the bedroom, the classmate ran to the sister said, there is a fellow looking for you. Sister is very puzzled, go out, see the younger brother Afar, wearing is covered with cement and sand overalls. Sister said, how do you and my classmates say 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? My sister's nose is sour and tears fall down. The younger brother quickly wiped away tears for her sister, said: "Sister, you do not cry, I come this time 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
#include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #define MAX 110using namespace Std;int n,m; int Vis[max][max];char map[max][max];int x1,x2,y1,y2;struct node{int x;int y;int step;friend bool operator < (node a,no De b) {return a.step>b.step;}}; void BFs (int x1,int y1,int x2,int y2) {int i,j,ok=0;int move[4][2]={0,1,0,-1,1,0,-1,0};p riority_queue<node>q; Node Beg,end;beg.x=x1;beg.y=y1;beg.step=0;q.push (Beg), while (!q.empty ()) {end=q.top (); Q.pop (); if (end.x==x2& &end.y==y2) {ok=1;break;} for (i=0;i<4;i++) {beg.x=end.x+move[i][0];beg.y=end.y+move[i][1];if (!vis[beg.x][beg.y]&&beg.x>=0 &&beg.x<n&&beg.y>=0&&beg.y<m&&map[beg.x][beg.y]!= ' # ') {if (map[beg.x][ Beg.y]>= ' A ' &&map[beg.x][beg.y]<= ' Z ') beg.step=end.step+map[beg.x][beg.y]-' a ' +1;elsebeg.step= End.step;vis[beg.x][beg.y]=1;q.push (Beg);}}} if (OK) printf ("%d\n", end.step),//The output here is to pay attention to whether the output beg.step or End.step elseprintf (" -1\n");} int mAin () {int j,i;int t;scanf ("%d", &t), while (t--) {scanf ("%d%d", &n,&m), for (i=0;i<n;i++) {scanf ("%s", map [i]);} for (i=0;i<n;i++) {for (j=0;j<m;j++) {if (map[i][j]== ' s ') {x1=i;y1=j;} if (map[i][j]== ' l ') {X2=i;y2=j;}}} memset (vis,0,sizeof (Vis)); BFs (x1,y1,x2,y2);}  return 0;}

  




Nyoj 1100 Wajueji which home strong!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.