想吃冰卻又敏感保護不足?來自星星的你也會打廣告?法人:14檔今年本業虧定了黃曉明、林志玲攜手代言...
2013-08-11 10:14:01 人氣(124) | 回應(0) | 推薦(0) | 收藏(0) 上一篇 | 下一篇

[UVA] 10284 - Chessboard in FEN

0
收藏
0
推薦

Problem B

Chessboard in FEN

Input: standard input

Output: standard output

Time Limit: 2 seconds

Memory Limit: 32 MB

 

In the FEN (Forsyth-Edwards Notation), a chessboard is described as follows:

  • The Board-Content is specified starting with the top row and ending with the bottom row.
  • Character / is used to separate data of adjacent rows.
  • Each row is specified from left to right.
  • White pieces are identified by uppercase piece letters: PNBRQK.
  • Black pieces are identified by lowercase piece letters: pnbrqk.
  • Empty squares are represented by the numbers one through eight.
  • A number used represents the count of contiguous empty squares along a row.
  • Each row's sum of numbers and characters must equal 8.

As for example:

5k1r/2q3p1/p3p2p/1B3p1Q/n4P2/6P1/bbP2N1P/1K1RR3, is the FEN notation description of the following chessboard:



The chessboard of the beginning of a chess game is described in FEN as:

rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR

 

Your task is simple: given a chessboard description in a FEN notation you are asked to compute the number of unoccupied squares on the board which are not attacked by any piece.

 

Input

Input is a sequence of lines, each line containing a FEN description of a chessboard. Note that the description does not necessarily give a legal chess position. Input lines do not contain white space.

 

Output

For each line of input, output one line containing an integer which gives the number of unoccupied squares which are not attacked.

 

Sample Input

5k1r/2q3p1/p3p2p/1B3p1Q/n4P2/6P1/bbP2N1P/1K1RR3
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR

 

Sample Output

3
16

(Math Lovers’ Contest, Source: University of Alberta Local Contest)

 

 

檢查不會被攻擊的格子個數。

#include <stdio.h>
#include <string.h>
void FEN(char s[], char g[][8]) {
    int i, row = 0, column = 0;
    for(i = 0; s[i]; i++) {
        if(s[i] == '/')
            row++, column = 0;
        else if(s[i] >= '0' && s[i] <= '9'){
            while(s[i] > '0') {
                g[row][column] = ' ', column++;
                s[i]--;
            }
        } else
            g[row][column] = s[i], column++;
    }
}
void color(int x, int y, char c, int attack[][8], char g[][8]) {
    if(c == 'p') {
        if(x+1 < 8 && y+1 < 8)
            attack[x+1][y+1] = 1;
        if(x+1 < 8 && y-1 >= 0)
            attack[x+1][y-1] = 1;
        return;
    }
    if(c == 'P') {
        if(x-1 >= 0 && y+1 < 8)
            attack[x-1][y+1] = 1;
        if(x-1 >= 0 && y-1 >= 0)
            attack[x-1][y-1] = 1;
        return;
    }
    c |= 32;
    int dx[] = {0,0,1,-1,1,1,-1,-1};
    int dy[] = {1,-1,0,0,1,-1,1,-1};
    int tx, ty, i;
    if(c == 'n') {
        int ddx[] = {2,2,-2,-2,1,1,-1,-1};
        int ddy[] = {1,-1,1,-1,2,-2,2,-2};
        for(i = 0; i < 8; i++) {
            tx = x+ddx[i], ty = y+ddy[i];
            if(tx < 0 || ty < 0 || tx >= 8 || ty >= 8)
                continue;
            attack[tx][ty] = 1;
        }
        return;
    }
    if(c == 'b') {
        for(i = 4; i < 8; i++) {
            tx = x+dx[i], ty = y+dy[i];
            while(tx >= 0 && tx < 8 && ty >= 0 && ty < 8) {
                if(g[tx][ty] != ' ')    break;
                attack[tx][ty] = 1;
                tx = tx+dx[i], ty = ty+dy[i];
            }
        }
        return;
    }
    if(c == 'r') {
        for(i = 0; i < 4; i++) {
            tx = x+dx[i], ty = y+dy[i];
            while(tx >= 0 && tx < 8 && ty >= 0 && ty < 8) {
                if(g[tx][ty] != ' ')    break;
                attack[tx][ty] = 1;
                tx = tx+dx[i], ty = ty+dy[i];
            }
        }
        return;
    }
    if(c == 'q') {
        for(i = 0; i < 8; i++) {
            tx = x+dx[i], ty = y+dy[i];
            while(tx >= 0 && tx < 8 && ty >= 0 && ty < 8) {
                if(g[tx][ty] != ' ')    break;
                attack[tx][ty] = 1;
                tx = tx+dx[i], ty = ty+dy[i];
            }
        }
        return;
    }
    if(c == 'k') {
        for(i = 0; i < 8; i++) {
            tx = x+dx[i], ty = y+dy[i];
            if(tx < 0 || ty < 0 || tx >= 8 || ty >= 8)
                continue;
            attack[tx][ty] = 1;
        }
        return;
    }
}
int main() {
    char s[999], g[8][8];
    int i, j;
    while(scanf("%s", s) == 1) {
        memset(g, ' ', sizeof(g));
        FEN(s, g);
        int attack[8][8] = {};
        for(i = 0; i < 8; i++) {
            for(j = 0; j < 8; j++) {
                if(g[i][j] != ' ')
                    color(i, j, g[i][j], attack, g);
            }
        }
        int ret = 0;
        for(i = 0; i < 8; i++)
            for(j = 0; j < 8; j++)
                if(g[i][j] == ' ' && attack[i][j] == 0)
                    ret++;
        printf("%d\n", ret);
        /*for(i = 0; i < 8; i++, puts(""))
            for(j = 0; j < 8; j++)
                printf("%c%d ", g[i][j], attack[i][j]);*/
    }
    return 0;
}

10284Chessboard in FEN
台長:Morris
人氣(124) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA] 1061 - Consanguine Calculations
此分類上一篇:[UVA] 10646 - What is the Card

我要回應
是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入以下數字 (ex:123)

(有*為必填)
詳全文