zoukankan      html  css  js  c++  java
  • Painter's Problem poj1681 高斯消元法

    Painter's Problem
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 4420   Accepted: 2143

    Description

    There is a square wall which is made of n*n small square bricks. Some bricks are white while some bricks are yellow. Bob is a painter and he wants to paint all the bricks yellow. But there is something wrong with Bob's brush. Once he uses this brush to paint brick (i, j), the bricks at (i, j), (i-1, j), (i+1, j), (i, j-1) and (i, j+1) all change their color. Your task is to find the minimum number of bricks Bob should paint in order to make all the bricks yellow. 

    Input

    The first line contains a single integer t (1 <= t <= 20) that indicates the number of test cases. Then follow the t cases. Each test case begins with a line contains an integer n (1 <= n <= 15), representing the size of wall. The next n lines represent the original wall. Each line contains n characters. The j-th character of the i-th line figures out the color of brick at position (i, j). We use a 'w' to express a white brick while a 'y' to express a yellow brick.

    Output

    For each case, output a line contains the minimum number of bricks Bob should paint. If Bob can't paint all the bricks yellow, print 'inf'.

    Sample Input

    2
    3
    yyy
    yyy
    yyy
    5
    wwwww
    wwwww
    wwwww
    wwwww
    wwwww
    

    Sample Output

    0
    15

      1 #include <iostream>
      2 #include <stdio.h>
      3 #include <algorithm>
      4 #include <math.h>
      5 #include <string.h>
      6 #include <set>
      7 using namespace std;
      8 const int MAXN=300;
      9 int a[MAXN][MAXN];
     10 int x[MAXN];
     11 bool free_x[MAXN];
     12 inline int gcd(int a,int b)
     13 {
     14     int t;
     15     while(b!=0)
     16     {
     17         t=b;
     18         b=a%b;
     19         a=t;
     20     }
     21     return a;
     22 }
     23 inline int lcm(int a,int b)
     24 {
     25     return a/gcd(a,b)*b;
     26 }
     27 int Gauss(int equ,int var)
     28 {
     29     int i,j,k;
     30     int max_r;
     31     int col;
     32     col=0;
     33     for(k = 0; k < equ && col < var; k++,col++)
     34     {
     35         max_r=k;
     36         for(i=k; i<equ; i++)
     37         {
     38             if(a[i][col])
     39             {
     40                 max_r=i;
     41                 break;
     42             }
     43         }
     44         if(max_r!=k)
     45         {
     46             for(j=k; j<var+1; j++) swap(a[k][j],a[max_r][j]);
     47         }
     48         if(a[k][col]==0)
     49         {
     50             k--;
     51             continue;
     52         }
     53         for(i=0; i<equ; i++)
     54         {
     55             if(i!=k&&a[i][col]!=0)
     56             {
     57                 for(j=0; j<var+1; j++)
     58                 {
     59                     a[i][j]^= a[k][j];
     60                 }
     61             }
     62         }
     63     }
     64     for (i = k; i < equ; i++)
     65     {
     66         if (a[i][col] != 0) return 0;
     67     }
     68     return 1;
     69 }
     70 int main()
     71 {
     72     int n,m,t,i,j;
     73     cin>>t;
     74     char x;
     75     while(t--)
     76     {
     77         memset(a,0,sizeof(a));
     78         cin>>n;
     79         m=n*n;
     80         for(i=0; i<m; i++)
     81         {
     82             if(i%n==0)getchar();
     83             x=getchar();
     84             if(x!='y')a[i][m]=1;
     85         }
     86         for(i=0; i<m; i++)
     87         {
     88             a[i][i]=1;
     89             if(i-n>=0)
     90                 a[i][i-n]=1;
     91             if(i+n<m)
     92                 a[i][i+n]=1;
     93             if(i%n)
     94                 a[i][i-1]=1;
     95             if((i+1)%n)
     96                 a[i][i+1]=1;
     97         }
     98         if(!Gauss(m,m))cout<<"inf"<<endl;
     99         else
    100         {
    101             int ans=0;
    102             for(i=0; i<m; i++)ans+=a[i][m]&1;
    103             cout<<ans<<endl;
    104         }
    105     }
    106 }
    View Code
  • 相关阅读:
    Docker容器(分享十五)
    oracle数据库迁移主从复制ADG(分享十四)
    mysql数据库迁移主从复制(分享十三)
    rsync+inotify实现服务器之间文件实时同步(分享十三)
    mysql数据库迁移(分享十二)
    数据库迁移(分享十一续集)
    Codeforces 1185G2 Playlist for Polycarp (hard version) 背包,暴力
    Codeforces 747F Igor and Interesting Numbers DP 组合数
    Codeforces 745E Hongcow Buys a Deck of Cards 状压DP / 模拟退火
    Codeforces 1140F 线段树 分治 并查集
  • 原文地址:https://www.cnblogs.com/ERKE/p/3835418.html
Copyright © 2011-2022 走看看