(DFS、bitset)AOJ-0525 Osenbei

题目地址

简要题意:

  给出n行m列的0、1矩阵,每次操作可以将任意一行或一列反转,即这一行或一列中0变为1,1变为0。问通过任意多次这样的变换,最多可以使矩阵中有多少个1。

思路分析:

  行数比较小,先不考虑对列的操作,将行数的所有情况举出来最多2^10种情况。对于已经固定了对行进行怎样的操作后,这种情况下对列的最优操作就是对每一列,如果此时1比0多就不变,不然就反转。实现在代码中就是一个for循环扫一遍。注意本题反转行、列采用bitset中的flip函数是非常合适的,因为每个数也不是1就是0,恰好符合位数。

参考代码:

  

 1 #include <iostream>
 2 #include <bitset>
 3 #include <algorithm>
 4
 5 using namespace std;
 6 bitset<10000>bing[10];//建立bitset数组储存数据,方便反转行列
 7 int main()
 8 {
 9     int hang,lie,i,j,ci,k,re=0;
10     bool tem;
11     while(cin>>hang>>lie&&hang>0)
12     {
13         re=0;
14         for(i=0;i<hang;i++)
15     {
16         for(j=0;j<lie;j++)
17         {
18             cin>>tem;
19             bing[i][j]=tem;
20         }
21     }
22     ci=1<<hang;//使用二进制的遍历已经是见了很多次的技巧了
23     for(i=0;i<ci;i++)
24     {
25         for(j=0;j<hang;j++)
26         {
27             if(i&(1<<j))
28             {
29                 bing[j].flip();//进行反转
30             }
31         }
32         int an=0,anhe=0;
33         for(j=0;j<lie;j++)
34         {
35             an=0;
36             for(k=0;k<hang;k++)
37             {
38                 if(bing[k][j])
39                 {
40                     an++;
41                 }
42             }
43             an=max(an,hang-an);
44             anhe+=an;
45         }
46         re=max(anhe,re);
47         for(j=0;j<hang;j++)
48         {
49             if(i&(1<<j))
50             {
51                 bing[j].flip();//再转回来
52             }
53         }
54     }
55     cout<<re<<"\n";
56     }
57     return 0;
58 }
时间: 2024-08-05 19:08:23

(DFS、bitset)AOJ-0525 Osenbei的相关文章

[LeetCode] Surrounded Regions(DFS、BFS)

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O O X X X O X X O X X After running your function, the board should

POJ-3275:Ranking the Cows(Floyd、bitset)

Ranking the Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3301   Accepted: 1511 Description Each of Farmer John's N cows (1 ≤ N ≤ 1,000) produces milk at a different positive rate, and FJ would like to order his cows according to

(DFS、全排列)POJ-2718 Smallest Difference

题目地址 简要题意: 给若干组数字,每组数据是递增的在0--9之间的数,且每组数的个数不确定.对于每组数,输出由这些数组成的两个数的差的绝对值最小是多少(每个数出现且只出现一次). 思路分析: 对于n个数,必定为分成两个位数分别为n/2和n-n/2的数时才可能取得差的绝对值最小.两组数分别进行全排列比较大小,这样比较次数最大为(10A5)*(5A5)=10!,在可以接受的范围内. 参考代码: 1 #include<stdio.h> 2 #include<cstring> 3 #in

Battle Over Cities (25)(DFS、连通图)

It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of

(DFS、全排列)POJ-3187 Backward Digit Sums

题目地址 简要题意: 输入两个数n和m,分别表示给你1--n这些整数,将他们按一定顺序摆成一行,按照杨辉三角的计算方式进行求和,求使他们求到最后时结果等于m的排列中字典序最小的一种. 思路分析: 不难推得第一行为n个数a1\a2\--\an时求得的和为i=0∑n-1 ai*(n-1Ci) 根据此公式,考虑到数据量比较小,只需要将原本按递增顺序依次排列好的1--n按next_permutation给出的递增全排列顺序逐个代入,如果结果与m相等就停止循环即可. 参考代码: 1 #include<st

搜索分析(DFS、BFS、递归、记忆化搜索)

搜索分析(DFS.BFS.递归.记忆化搜索) 1.线性查找 在数组a[]={0,1,2,3,4,5,6,7,8,9,10}中查找1这个元素. (1)普通搜索方法,一个循环从0到10搜索,这里略. (2)递归(从中间向两边) 1 //递归一定要写成记忆化递归 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool vis[11]; 5 int count1=0; 6 7 void search(int n){ 8 count1++; 9

poj1088 滑雪(dfs、dp优化)

#include <iostream> #include <map> #include <string> #include <cstdio> #include <sstream> #include <cstring> #include <vector> #include <cmath> #define N 110 int a,b,step=0; int anw=0; int moun[N][N]; int dp

[转] AOJ 0525 Osenbei《挑战程序设计竞赛(第2版)》练习题答案

来自 码农场 ? AOJ 0525 Osenbei<挑战程序设计竞赛(第2版)>练习题答案 只把代码复制过来,原博的其他分析请看链接. 1 #include <iostream> 2 #include <bitset> 3 #include <algorithm> 4 5 using namespace std; 6 7 bitset<10000> cookie[10]; 8 9 ///////////////////////////SubMai

图基本算法 图搜索(广度优先、深度优先)

(边自学边写,还真有点累啊,) 注:以下代码均为部分,关于图的表示方法参看我的博客: http://www.cnblogs.com/dzkang2011/p/graph_1.html 一.广度优先搜索 广度优先搜索(BFS)是最简单的图搜索算法之一,也是很多重要的图算法的原型.在Prim最小生成树算法和Dijkstra单源最短路径算法中,都采用了与广度优先搜索类似的思想. 在给定图G=(V,E)和一个特定的源顶点s的情况下,广度优先搜索系统地探索G中的边,以期发现可以从s到达的所有顶点,并计算s