24h購物| | PChome| 登入
2013-08-28 07:21:26| 人氣1,640| 回應0 | 上一篇 | 下一篇

[UVA][bfs] 10097 - The Color Game

推薦 0 收藏 0 轉貼0 訂閱站台

Problem B

The Color Game

Input: standard input

Output: standard output

 

In this problem, you will get introduced to a new game called the Color Game. In the N-cell  (3 <= N <= 100) color game, there are N cells each having a different color. For simplicity, we will assume that the colors are represented by unique positive integers ranging from 1 to N. Each cell has at most one edge (directed) of each color running to another or even the same cell. This is a two-player game and it consists of two phases. In the first phase one of the players plays and in the second phase plays the other.

 

Suppose player 1 plays in the first phase. At the beginning, player 2 selects three cells N1, N2 and N3, and places two tokens in N1 and N2 respectively.  Now he challenges player 1 to move any one of the tokens to cell N3 in as few moves as possible. In each move only one of the two tokens can be moved. A token can move from the current cell to an adjacent cell only following an edge of the same color as that of the cell the other token is in. At the end of the phase, player 2 must prove that there is a way of moving one of the tokens to cell N3 otherwise he will lose. The second phase is similar to the first one except that the players are now reversed. The player solving the problem in fewer moves wins the game.

 

Now, given the description of the network of cells and the values of N1, N2 and N3, you are asked to write a program to determine the minimum number of moves required to moves any of the tokens to cell N3.

 

Input

The input file consists of several data blocks. Each data block describes a game.

 

The first line of a data block contains an integer N (3 <= N <= 100) representing the number of cells. Then follows N lines of N integers each. The j-th integer in the i-th line (1 <= i, j <= N) gives the cell number to which cell i is connected by an edge of color j. If cell i does not have an edge of color j, then this integer has a value 0. The last line of the data block contains the three integers: N1, N2 and N3.

 

The input file terminates with a zero for N.

 

Output

For each game in the input first output the game number followed by the minimum number of moves required to solve it. Print the line "Destination is Not Reachable !" if the problem is not solvable. Print a blank line after the outputs for each data set.

 

Sample Input

5
2 5 3 5 5
0 2 1 3 0
0 1 3 3 4
1 5 2 2 5
5 4 0 5 0
5 3 1
6
0 0 5 4 0 1
6 0 1 3 4 4
5 0 5 0 2 6
3 1 0 4 5 5
3 2 2 4 6 4
1 2 5 2 0 0
3 2 6
0

Sample Output

Game #1
Destination is Not Reachable !

Game #2
Minimum Number of Moves = 6


Rezaul Alam Chowdhury

這題很挑戰閱讀能力,顏色跟點差不多的東西。

N 個方格,又有 N 個不同顏色,大致上就猜一下點跟顏色是指同一個。

然後題目要問有兩個塊(token),移動其中一個塊到下一個地方時,會根據這兩塊所在的格子決定。

也就是說 g[a][b] 表示為第一塊 a, 第二塊 b 時,可以將 a 移動到 g[a][b]


也就是說 g[b][a] 表示為第一塊 a, 第二塊 b 時,可以將 b 移動到 g[b][a]


目標要求出其中一塊到 N3 的最少次數。

#include <stdio.h>
#include <queue>
#include <algorithm>
using namespace std;
int g[105][105];
int N1, N2, N3;
int bfs() {
    int dist[105][105] = {};
    int token1, token2;
    queue<int> T1, T2;//token;
    T1.push(N1), T2.push(N2);
    dist[N1][N2] = 1;
    while(!T1.empty()) {
        token1 = T1.front(), T1.pop();
        token2 = T2.front(), T2.pop();
        if(token1 == N3 || token2 == N3)
            return dist[token1][token2];
        if(g[token1][token2]) {//move token1
            if(dist[g[token1][token2]][token2] == 0) {
                dist[g[token1][token2]][token2] = dist[token1][token2]+1;
                T1.push(g[token1][token2]);
                T2.push(token2);
            }
        }
        if(g[token2][token1]) {//move token2
            if(dist[g[token2][token1]][token1] == 0) {
                dist[g[token2][token1]][token1] = dist[token1][token2]+1;
                T1.push(g[token2][token1]);
                T2.push(token1);
            }
        }
    }
    return 0;
}
int main() {
    int n, cases = 0;
    int i, j;
    while(scanf("%d", &n) == 1 && n) {
        for(i = 1; i <= n; i++)
            for(j = 1; j <= n; j++)
                scanf("%d", &g[i][j]);
        scanf("%d %d %d", &N1, &N2, &N3);
        int mn = bfs();
        printf("Game #%d\n", ++cases);
        if(mn == 0)
            puts("Destination is Not Reachable !");
        else
            printf("Minimum Number of Moves = %d\n", mn-1);
        puts("");
    }
    return 0;
}

台長: Morris
人氣(1,640) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][bfs] 10923 - Seven Seas
此分類上一篇:[UVA][dp] 1057 - Routing

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文