HDU 4146 Filp Game

原创 2018年04月16日 22:56:54

http://acm.hdu.edu.cn/showproblem.php?pid=4146
题目意思很简单,就是给你一个N*N的正方形,里面有N*N个格子,每个格子里面有一张纸片,这张纸片共有两面,White和Black,然后让你求出某一行或者某一列的纸片被翻转后,最终还有多少张纸片是白面朝上

我们要是直接暴力遍历的话,肯定会超时的
1 <= N <= 1000, 0 <= Q <= 100000
看这个数据范围,就是限制你不让你用暴力求解

其实解决方法也很简单,我们只需把为每行和每列的0索引值空出来,用来记录该行或者该列是否被翻转了即可
具体代码如下:

#include<stdio.h>
#include<string.h>

int wnum[1001][1001];
char square[1001][1001];
int main() {
    int T;
    scanf("%d", &T);
    int kase = 0;
    while(T--) { 
        int N;
        scanf("%d", &N);
        memset(square, '\0', sizeof(square));
        memset(wnum, 0, sizeof(wnum));
        for(int i = 1; i <= N; i++)
            scanf("%s", square[i]); 
        for(int i = 1; i <= N; i++)  
            for(int j = 1; j <= N; j++) 
                if(square[i][j-1] == 'w')  
                    wnum[i][j] = 1; 
        int n, a, b;
        scanf("%d", &n);
        for(int i = 0; i < n; i++) {
            scanf("%d%d", &a, &b);
            wnum[a][0] = !wnum[a][0];
            wnum[0][b] = !wnum[0][b];
        }
        for(int i = 1; i <= N; i++) {
            if(wnum[i][0])
                for(int j = 1; j <= N; j++)
                    wnum[i][j] = !wnum[i][j]; 
            if(wnum[0][i])
                for(int j = 1; j <= N; j++)
                    wnum[j][i] = !wnum[j][i]; 
        }
        int sum = 0;
        for(int i = 1; i <= N; i++) 
            for(int j = 1; j <= N; j++)  
                if(wnum[i][j])
                    sum++; 
        printf("Case #%d: %d\n", ++kase, sum);
    }
    return 0;
}
版权声明:欢迎提问:3079238667@qq.com https://blog.csdn.net/include_heqile/article/details/79968131

101个Java比.NET好的理由(1~10) (译者:陈佳新)

101个Java比.NET好的理由(1~10) (译者:陈佳新)   101 Reasons Java is Better than .NET (1~10)   作者:John Lambert   译...
  • bruce
  • bruce
  • 2003-02-09 11:01:00
  • 1882

HDU 4146 Flip Game

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4146 靠,题目意思看了半个多小时终于看懂了(其实是半猜懂的)。题目意思是说在一个x*y的方阵里分为白...
  • A_Eagle
  • A_Eagle
  • 2011-12-21 19:50:38
  • 1598

HDU4146 Flip Game

Flip Game Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Tot...
  • d_x_d
  • d_x_d
  • 2016-02-23 20:35:48
  • 546

hdu 4146 Flip Game

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4146 Flip Game Time Limit: 15000/5000 MS (Java/Othe...
  • qiqi_skystar
  • qiqi_skystar
  • 2016-02-29 21:43:07
  • 988

HDU 4146 Flip game

Flip Game Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) ...
  • Love_CppAndC
  • Love_CppAndC
  • 2012-02-09 16:47:11
  • 1190

LeetCode 293. Flip Game(反转游戏)

原题网址:https://leetcode.com/problems/flip-game/ You are playing the following Flip Game with your...
  • jmspan
  • jmspan
  • 2016-04-16 06:11:22
  • 591

Filp Game

Description   Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each o...
  • Smile__wei
  • Smile__wei
  • 2017-02-07 20:54:54
  • 128

poj 1753 Flip Game(枚举,bfs)(简单)

poj 1753 Flip Game题目http://poj.org/problem?id=1753 给你一个4*4的棋盘,每个棋子非黑即白,翻一个棋子会带动它上下左右的棋子也翻过来,问至少多少...
  • pibaixinghei
  • pibaixinghei
  • 2016-04-25 16:59:01
  • 1667

HDU 2053 Switch Game(数学题)

Switch Game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To...
  • liangzhaoyang1
  • liangzhaoyang1
  • 2016-05-07 23:18:34
  • 2512

POJ 1753 Flip Game (DFS 太暴力了!)

Flip Game Description Flip game is played on a rectangular 4x4 field with two-sided pieces pla...
  • u013476556
  • u013476556
  • 2014-06-14 20:22:12
  • 3892
收藏助手
不良信息举报
您举报文章:HDU 4146 Filp Game
举报原因:
原因补充:

(最多只允许输入30个字)