24h購物| | PChome| 登入
2012-07-18 14:25:47| 人氣1,086| 回應0 | 上一篇 | 下一篇

[UVA] 255 - Correct Move

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


 Correct Move 

We have a square board with 64 places, numbered from 0 to 63, see Figure 1. There are two pieces: A king and a queen. The pair of king's place and queen's place is called the state. A state is legal if the pieces are not at the same place. The king and queen move alternatingly. The king can move one step in a horizontal or vertical direction, as long as it does not arrive at the place of the queen. The queen can move one ore more steps in a horizontal or vertical direction, as long as it does not encounter the king. All these moves are called legal. Please note that the pieces may not move diagonally.

tabular24

Figure 1.

For example, suppose we have a state with a king at place 17 and a queen at place 49, as in Figure 2. The legal moves of the king are to places 9, 16, 18, and 25 and the queen can legally move to place 25, 33, 41, 48, 50, 51, 52, 53, 54, 55, or 57. We impose, however, an extra restriction: A piece may not move to a place where the other one can also move to. Legal moves satisfying this restriction are called allowed. In Figure 2, all possible places the king and the queen can move to by an allowed move, are denoted with a circle ( tex2html_wrap_inline187 ) and a dot ( tex2html_wrap_inline189 ), respectively. In Figure 3, the king cannot move, it is locked in.

tex2html_wrap215 \ tex2html_wrap217

This problem requires you to write a program that does some checking related to the movement of the queen.

Input

The input for your program resides in a textfile that ends with the standard end-of-file marker. Each line ends with the standard end-of-line marker and contains a sequence of three integers in the range O..63, separated by one space. The first two integers denote the place of the king and the queen, respectively. Together they form a state. The third integer denotes a new place for the queen. You may think of it as computed by some function movequeen. Your program determines whether:

  • the given state is legal
  • the queen's move is legal
  • the queen's move is allowed.

Furthermore, if these requirements are met, your program determines whether the move of the queen results in a state where the king is locked in.

Output

The output is also a textflle. For each input line, your program produces one output line with one of the following messages:

  • Illegal state
  • Illegal move
  • Move not allowed
  • Continue
  • Stop

`Illegal state' indicates that the given state is not legal, i.e. the pieces are at the same place.

`Illegal move' means that the given state is legal, but the queen's move is illegal.

`Move not allowed' applies if both the given state and the queen's move are legal, but the queen's move is not allowed.

Both `Continue' and `Stop' mean that the given state is legal and the queen's move is allowed. If the king can do an allowed move in the resulting state, the message is `Continue', otherwise the king is locked in and you reply `Stop'.

Sample Input

 17 17 49
 17 49 56
 17 49 9
 17 49 17
 17 49 25
 17 49 33
 17 49 41
 17 49 49
 56 48 49

Sample Output

Illegal state
Illegal move
Illegal move
Illegal move
Move not allowed
Continue
Continue
Illegal move
Stop

英文很不好理解在於 它的各項輸出條件
Illegal state 一開始國王跟皇后同一個位置
Illegal move 皇后可以上下左右移動的條件, 但不可飛過國王 (也就是跳過去)
Move not allowed 皇后無法移動指定位置 (它給定的條件, 到的地方不可是國王的移動區域)
Continue and Stop 決定於 皇后走完後, 國王還能不能走

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
    int K, Q, M;
    while(scanf("%d %d %d", &K, &Q, &M) == 3) {
        if(K == Q) {
            puts("Illegal state");
            continue;
        }
        int map[8][8] = {}, i, j;
        int x, y, r = 0, l = 0, u = 0, d = 0;
        x = K/8, y = K%8;
        map[x][y] = 1; /*King*/
        x = Q/8, y = Q%8;
        for(i = 1; i <= 7; i++) {
            if(r == 0 && !(x+i < 0 || x+i > 7)) {
                if(map[x+i][y] == 1)
                    r = 1;
                else
                    map[x+i][y] = 1;
            }
            if(l == 0 && !(x-i < 0 || x-i > 7)) {
                if(map[x-i][y] == 1)
                    l = 1;
                else
                    map[x-i][y] = 1;
            }
            if(u == 0 && !(y+i < 0 || y+i > 7)) {
                if(map[x][y+i] == 1)
                    u = 1;
                else
                    map[x][y+i] = 1;
            }
            if(d == 0 && !(y-i < 0 || y-i > 7)) {
                if(map[x][y-i] == 1)
                    d = 1;
                else
                    map[x][y-i] = 1;
            }
        }
        if(map[M/8][M%8] == 0 || M == K) {
            puts("Illegal move");
            continue;
        }
        if(abs(K/8-M/8)+abs(K%8-M%8) == 1) {
            puts("Move not allowed");
            continue;
        }
        int stop = 0;
        x = K/8, y = K%8;
        if(x+1 > 7 || x+1 == M/8)   stop++;
        if(x-1 < 0 || x-1 == M/8)   stop++;
        if(y+1 > 7 || y+1 == M%8)   stop++;
        if(y-1 < 0 || y-1 == M%8)   stop++;
        if(stop == 4)
            puts("Stop");
        else
            puts("Continue");
    }
    return 0;
}

台長: Morris
人氣(1,086) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 227 - Puzzle
此分類上一篇:[UVA][Java] 288 - Arithmetic Operations With Large Integers

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