zoukankan      html  css  js  c++  java
  • HDU4146 Flip Game

    Flip Game

    Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
    Total Submission(s): 824    Accepted Submission(s): 283


    Problem Description
    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.
     
    Input
    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).
     
    Output
    For each case, please print the case number (beginning with 1) and the number of white pieces after sequences of commands.
     
    Sample Input
    2 4 bwww wbww wwbw wwwb 2 1 1 4 4 4 wwww wwww wwww wwww 1 1 1
     
    Sample Output
    Case #1: 4 Case #2: 10
      1 /*
      2 //代码一:-----TLE
      3 #include<stdio.h>
      4 #include<string.h>
      5 char str[1005][1005];
      6 int cnt[1005][1005];
      7 
      8 int main()
      9 {
     10     int T,i,j,k,n,m,sum,row,col;
     11     scanf("%d",&T);
     12     for(k=1;k<=T;++k)
     13     {
     14         scanf("%d",&n);
     15         getchar();
     16         for(i=0;i<n;++i)
     17         {
     18             for(j=0;j<n;++j)
     19             {
     20                 scanf("%c",&str[i][j]);
     21                 cnt[i][j]=0;
     22             }
     23             getchar();
     24         }
     25         scanf("%d",&m);
     26         while(m--)
     27         {
     28             scanf("%d%d",&row,&col);
     29             --row;
     30             --col;
     31             for(i=0;i<n;++i)
     32             {
     33                 ++cnt[row][i];
     34                 ++cnt[i][col];
     35             }
     36         }
     37         sum=0;
     38         for(i=0;i<n;++i)
     39             for(j=0;j<n;++j)
     40             {
     41                 if(str[i][j]=='b'&&(cnt[i][j]&1))
     42                     ++sum;
     43                 if(str[i][j]=='w'&&!(cnt[i][j]&1))
     44                     ++sum;
     45             }
     46         printf("Case #%d: %d\n",k,sum);
     47     }
     48     return 0;
     49 }
     50 */
     51 
     52 //代码二:------WA----看半天看不出来哪错了,跪求路过的拯救
     53 #include<stdio.h>
     54 #include<string.h>
     55 char str[1005][1005];
     56 
     57 int main()
     58 {
     59     int T,i,j,k,n,m,sum,row,col;
     60     scanf("%d",&T);
     61     for(k=1;k<=T;++k)
     62     {
     63         sum=0;
     64         scanf("%d",&n);
     65         getchar();
     66         for(i=0;i<n;++i)
     67         {
     68             for(j=0;j<n;++j)
     69             {
     70                 scanf("%c",&str[i][j]);
     71                 if(str[i][j]=='w')
     72                     ++sum;
     73             }
     74             getchar();
     75         }
     76         scanf("%d",&m);
     77         while(m--)   //输入一条指令更新计算一次
     78         {
     79             scanf("%d%d",&col,&row);
     80             --row;
     81             --col;
     82             for(i=0;i<n;++i)
     83             {
     84                 if(str[row][i]=='b')
     85                 {
     86                     ++sum;
     87                     str[row][i]='w';
     88                 }
     89                 else
     90                 {
     91                     --sum;
     92                     str[row][i]='b';
     93                 }
     94                 if(str[i][col]=='b')
     95                 {
     96                     ++sum;
     97                     str[i][col]='w';
     98                 }
     99                 else
    100                 {
    101                     --sum;
    102                     str[i][col]='b';
    103                 }
    104             }
    105         }
    106         printf("Case #%d: %d\n",k,sum);
    107     }
    108     return 0;
    109 }
    110 
    111 /*
    112 //代码三:-----AC
    113 #include<cstdio>
    114 #include<cstring>
    115 char grid[1005][1005];
    116 int col[1005],row[1005];  //分别记录该列和该行变化的次数为奇数次还是偶数次,用1和0表示(异或运算)
    117 int main()
    118 {
    119     int T,r,n,Q,a,b,i,j;
    120     scanf("%d",&T);
    121     for(r=1;r<=T;++r)
    122     {
    123         scanf("%d",&n);
    124         for(int i=0; i<n; i++)
    125             scanf("%s",grid[i]);
    126         scanf("%d",&Q);
    127         memset(col,0,sizeof(col));
    128         memset(row,0,sizeof(row));
    129         for(i=0;i<Q;i++)
    130         {
    131             scanf("%d%d",&a,&b);
    132             --a;
    133             --b;
    134             row[a]^=1;
    135             col[b]^=1;
    136         }
    137         int res=0;
    138         for(i=0;i<n;i++)
    139         {
    140             for(j=0;j<n;j++)
    141             {
    142                 if((row[i]+col[j])&1) 
    143                 {
    144                     if(grid[i][j]=='b') 
    145                         res++;
    146                 }
    147                 else 
    148                 {
    149                     if(grid[i][j]=='w') 
    150                         res++;
    151                 }
    152             }
    153         }
    154         printf("Case #%d: %d\n",r,res);
    155     }
    156     return 0;
    157 }
    158 */
    功不成,身已退
  • 相关阅读:
    hdu1085
    hdu1028
    hdu2189
    母函数
    博弈论
    nginx安装
    学习好站点
    nginx在linux下安装
    wget 命令用法详解
    U盘安装CentOS7的帖子
  • 原文地址:https://www.cnblogs.com/dongsheng/p/2675424.html
Copyright © 2011-2022 走看看