Nyoj1100 wajueji which home strong! [BFS]

Source: Internet
Author: User

Wajueji which home strong! Time Limit: 1000 MS | memory limit: 65535 kb difficulty: 2
Description

In a ravine, the two sisters were admitted to the university at the same time. However, this is not good news because of family constraints. My father said to the child, "I just want to give up your sister-in-law. At that time, my sister had decided to give up the opportunity to go to school. I didn't expect that the next day would not be bright, so my younger brother had to secretly carry a few broken clothes and a few kiba steamed buns and left a note on my sister's pillow: Sister, don't worry, it is not easy to go to college. I am going out to work for you. Brother. My sister held the word, and squatted on the mirror, crying. That year, my younger brother was 17 years old and my sister was 20 years old. My sister spent the money she borrowed from her father's village and her younger brother who moved cement at the construction site. One day, my sister was reading a book in the dormitory, And the classmate ran in and told her that a fellow was looking for you. My sister wondered, when I went out, I saw my younger brother far away, dressed in concrete and sand overalls. My sister said, How did you tell my classmates that you are my hometown? He smiled and said, You see me wear this, saying it's your brother. Your classmates are not laughing at you yet? My sister's nose was sour and tears fell. My younger brother quickly wiped her tears and said, "Don't cry, sister. I want to ask you to find out which excavator is the best?

 

With your help, my brother has set foot on the road to lanxiang.

The problem arises.

Input
The first number of T, T group test data.
Two numbers n, m; (0 <n, m <= 100) represents a two-dimensional map of M columns in the H row.
The next n rows contain M characters in each line.
'S' indicates the current position of the younger brother.
'#' Indicates a mountain. In order to save physical strength, do not pass from here.
'A'-'Z' indicates the economic level of each region, corresponding to 1-26. Living expenses must be paid when passing through the area with the corresponding characters.
'L' indicates the location of the lanxiang technical school.
Both S and l are lowercase letters.
The younger brother can only take four directions.
Output
Output a number to indicate the minimum living expenses required for the younger brother to arrive at lanxiang.
If it cannot be reached, output-1.
Sample Input
33 5#sVGFA##ZAlCDBC3 3sABABSABl3 3s#B###ABl
Sample output
484-1
Source
Current year
Uploaded
ACM _ An pengcheng
Excavator which is strong...


 #include <stdio.h>#include <string.h>#include <queue>#define maxn 105int n, m;char G[maxn][maxn];bool vis[maxn][maxn];const int mov[][2] = {1, 0, -1, 0, 0, 1, 0, -1};struct Node {int x, y, w;friend bool operator<(Node a, Node b) {return a.w > b.w;}} s;void getMap() {int i, j;char *p;for(i = 0; i < n; ++i) {scanf("%s", G[i]);if(p = strchr(G[i], 's')) {s.x = i; s.y = p - G[i];}}s.w = 0;}int BFS() {std::priority_queue<Node> Q;Q.push(s);Node u, v;int x, y, i;memset(vis, 0, sizeof(vis));vis[s.x][s.y] = 1;while(!Q.empty()) {u = Q.top(); Q.pop();for(i = 0; i < 4; ++i) {x = u.x + mov[i][0];y = u.y + mov[i][1];if(x >= 0 && x < n && y >= 0 && y < m && G[x][y] != '#' && !vis[x][y]) {v = u;v.x = x;v.y = y;if(G[x][y] == 'l')return v.w;v.w += G[x][y] - 'A' + 1;vis[x][y] = 1;Q.push(v);}}}return -1;}int main() {// freopen("stdin.txt", "r", stdin);int t;scanf("%d", &t);while(t--) {scanf("%d%d", &n, &m);getMap();printf("%d\n", BFS());}return 0;}         


Nyoj1100 wajueji which home strong! [BFS]

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.