51nod1445(最短路)

题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1445

 

题意: 中文题诶~

 

思路: 可以将每种颜色看作一个节点, 从一种颜色到另一种颜色的代价看作这两个节点之间的距离, 即 x 节点到 y 节点的距离为行号为 x 的行中 0 ~ y - 1 中的 Y 的数目. 然后再跑一遍最短路即可.

 

代码:

 1 #include <iostream>
 2 #include <stdio.h>
 3 using namespace std;
 4 
 5 const int inf = 1e9;
 6 const int MAXN = 60;
 7 int mp[MAXN][MAXN];
 8 
 9 void floyd(int n){
10     for(int k = 0; k < n; k++){
11         for(int i = 0; i < n; i++){
12             for(int j = 0; j < n; j++){
13                 mp[i][j] = min(mp[i][j], mp[i][k] + mp[k][j]);
14             }
15         }
16     }
17 }
18 
19 int main(void){
20     char ch;
21     int t, n;
22     cin >> t;
23     while(t--){
24         cin >> n;
25         for(int i = 0; i < n; i++){
26             int cnt = 0;
27             for(int j = 0; j < n; j++){
28                 cin >> ch;
29                 if(ch == 'Y') mp[i][j] = cnt, cnt++;
30                 else mp[i][j] = inf;
31             }
32         }
33         floyd(n);
34         printf("%d\n", mp[0][n - 1] >= inf ? -1 : mp[0][n - 1]);
35     }
36     return 0;
37 }
View Code

 

posted @ 2017-09-11 16:33  geloutingyu  阅读(207)  评论(0编辑  收藏  举报