首页 > ACM题库 > HDU-杭电 > HDU 4146-Flip Game[解题报告]HOJ
2015
04-16

HDU 4146-Flip Game[解题报告]HOJ

Flip Game

问题描述 :

Flip game is played on a square N*N field with two-sided pieces placed on each of its N^2 squares. One side of each piece is white and the other one is black and each piece is lying either it’s black or white side up. The rows are numbered with integers from 1 to N upside down; the columns are numbered with integers from 1 to N from the left to the right. Sequences of commands (xi, yi) are given from input, which means that both pieces in row xi and pieces in column yi will be flipped (Note that piece (xi, yi) will be flipped twice here). Can you tell me how many white pieces after sequences of commands?
Consider the following 4*4 field as an example:

bwww
wbww
wwbw
wwwb

Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up.
Two commands are given in order: (1, 1), (4, 4). Then we can get the final 4*4 field as follows:

bbbw
bbwb
bwbb
wbbb

So the answer is 4 as there are 4 white pieces in the final field.

输入:

The first line contains a positive integer T, indicating the number of test cases (1 <= T <= 20).
For each case, the first line contains a positive integer N, indicating the size of field; The following N lines contain N characters each which represent the initial field. The following line contain an integer Q, indicating the number of commands; each of the following Q lines contains two integer (xi, yi), represent a command (1 <= N <= 1000, 0 <= Q <= 100000, 1 <= xi, yi <= N).

输出:

The first line contains a positive integer T, indicating the number of test cases (1 <= T <= 20).
For each case, the first line contains a positive integer N, indicating the size of field; The following N lines contain N characters each which represent the initial field. The following line contain an integer Q, indicating the number of commands; each of the following Q lines contains two integer (xi, yi), represent a command (1 <= N <= 1000, 0 <= Q <= 100000, 1 <= xi, yi <= N).

样例输入:

2
4
bwww
wbww
wwbw
wwwb
2
1 1
4 4
4
wwww
wwww
wwww
wwww
1
1 1

样例输出:

Case #1: 4
Case #2: 10

#include<iostream>
#include<cstring>
#include<cstdio>

using namespace std;

const int N = 1111;
char map[N][N];
bool r[N], c[N];

int main()
{
    int t, n, i, j, m, x, y, cas, ans;
    scanf("%d", &t);
    for(cas = 1; cas <= t; cas++)
    {
        scanf("%d", &n);
        for(i = 0; i < n; i++)
            scanf("%s", map[i]);

        scanf("%d", &m);
        memset(r, 0, sizeof(r));
        memset(c, 0, sizeof(c));
        for(i = 0; i < m; i++)
        {
            scanf("%d%d", &x, &y);
            r[--x] = !r[x];
            c[--y] = !c[y];
        }
        ans = 0;
        for(i = 0; i < n; i++)
            for(j = 0; j < n; j++)
                if((r[i] + c[j] != 1 && map[i][j] == 'w') || (r[i] + c[j] == 1 && map[i][j] == 'b'))
                    ans++;
        printf("Case #%d: %d\n", cas, ans);
    }
}