[水题]ZOJ3038 Triangle War II

题意:

给了这样一张图 有两种状态:pushed(*)和unpushed(.)    为方便起见分别成为 开 和 关

改变一个点的开关状态 会同时改变与它相邻的点的开关状态  比如改变5,则2、3、4、6、8、9都会改变

N(行数)最多为6 即 最多21个点

求: 任意改变开关状态后 最多能有几个关着。

为什么这么像高斯消元!!!

每个点的状态只有开和关 总共就只有2^21个状态

dfs会爆栈!

所以bfs就行了! 把这些点的开关状态看成二进制 用0到2^21的数组来标记当前状态有没有出现过

  1 struct node
  2 {
  3     bool a[25];
  4 }front, rear;
  5 queue<node> q;
  6 int ans;
  7 bool vis[2100000];
  8 int biao[]={0, 1, 3, 6, 10, 15, 21};
  9 int b[][2]={
 10             {0,0},
 11             {-1,0},
 12             {-1,-1},
 13             {1,0},
 14             {1,1},
 15             {0,1},
 16             {0,-1}
 17            };
 18 int l[25], c[25][10];
 19 void pre()
 20 {
 21     memset(l, 0, sizeof(l));
 22     for(int i=1;i<=21;i++)
 23     {
 24         int x=0;
 25         while(biao[x]<i)
 26             x++;
 27         int y=i-x*(x-1)/2;
 28         for(int k=0;k<7;k++)
 29         {
 30             int dx=x+b[k][0];
 31             int dy=y+b[k][1];
 32             if(dx>=1 && dx<=6 && dy>=1 && dy<=dx)
 33                 c[i][l[i]++]=dx*(dx-1)/2+dy;
 34         }
 35     }
 36 }
 37 void bfs(int n)
 38 {
 39     int d=0;
 40     for(int i=1;i<=n;i++)
 41         d=(d<<1)+front.a[i];
 42     vis[d]=1;
 43     q.push(front);
 44     while(!q.empty())
 45     {
 46         front=q.front();
 47         q.pop();
 48         for(int i=1;i<=n;i++)
 49             if(!front.a[i])
 50             {
 51                 rear=front;
 52                 for(int j=0;j<l[i];j++)
 53                     rear.a[c[i][j]]=1^front.a[c[i][j]];
 54                 d=0;
 55                 int cnt=0;
 56                 for(int j=1;j<=n;j++)
 57                 {
 58                     d=(d<<1)+rear.a[j];
 59                     if(!rear.a[j])
 60                         cnt++;
 61                 }
 62                 if(!vis[d])
 63                 {
 64                     vis[d]=1;
 65                     ans=max(ans, cnt);
 66                     if(ans==n)
 67                         return ;
 68                     q.push(rear);
 69                 }
 70             }
 71     }
 72 }
 73 int main()
 74 {
 75     pre();
 76     int n;
 77     while(~scanf("%d", &n))
 78     {
 79         ans=0;
 80         for(int i=1;i<=n;i++)
 81             for(int j=1;j<=i;j++)
 82             {
 83                 char ch;
 84                 cin>>ch;
 85                 if(ch==‘.‘)
 86                     ans++;
 87                 front.a[i*(i-1)/2+j]=(ch==‘*‘);
 88             }
 89         n=(n+1)*n/2;
 90         if(ans==n)
 91         {
 92             printf("%d\n", ans);
 93             continue;
 94         }
 95         while(!q.empty())
 96             q.pop();
 97         memset(vis, 0, sizeof(vis));
 98         bfs(n);
 99         printf("%d\n", ans);
100     }
101     return 0;
102 }

ZOJ 3038

时间: 2024-10-26 00:45:16

[水题]ZOJ3038 Triangle War II的相关文章

H. 硬币水题II

H. 硬币水题II Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main Submit Status PID: 29064 Font Size:  +   - 小胖有一个正反面不对称的硬币.如果抛一次这个硬币,它的正面朝上的概率为p,反面朝上的概率为1-p.现在,小胖想用这个硬币来产生等概率的决策(50%对

BNU29064——硬币水题II——————【事件概率】

硬币水题II Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main Submit Status PID: 29064 小胖有一个正反面不对称的硬币.如果抛一次这个硬币,它的正面朝上的概率为p,反面朝上的概率为1-p.现在,小胖想用这个硬币来产生等概率的决策(50%对50%).当然,只抛一次是不行的.小胖的策略是这样的:每一次决策,需要抛硬币两次,如果都是

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

[Leetcode][JAVA] Pascal&#39;s Triangle I, II

Pascal's Triangle: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 已知行数生成帕斯卡三角.实际上只要有第i层,那么就能生成第i+1层.每次新生成的层加入最终集合中即可. 1 public List<List<Integer&g

leetcode -day13 Valid Palindrome &amp; Triangle &amp; Pascal&#39;s Triangle I II

1.  Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note:

HDU4891_The Great Pan_字符串水题

2014多校第五题,当时题面上的10^5写成105,我们大家都wa了几发,改正后我和一血就差几秒…不能忍 题目:http://acm.hdu.edu.cn/showproblem.php?pid=4891 The Great Pan Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 966    Accepted Submission(s

poj 1085 Triangle War (状压+记忆化搜索)

Triangle War Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2685   Accepted: 1061 Description Triangle War is a two-player game played on the following triangular grid: Two players, A and B, take turns filling in any dotted line connect

hdu 2393:Higher Math(计算几何,水题)

Higher Math Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2219    Accepted Submission(s): 1219 Problem Description You are building a house. You'd prefer if all the walls have a precise right

HDU 4022 Bombing(基本算法-水题)

Bombing Problem Description It's a cruel war which killed millions of people and ruined series of cities. In order to stop it, let's bomb the opponent's base. It seems not to be a hard work in circumstances of street battles, however, you'll be encou