zoukankan      html  css  js  c++  java
  • ZOJ

    先上题目:

    Paint the Grid Again

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or white).

    Leo has a magical brush which can paint any row with black color, or any column with white color. Each time he uses the brush, the previous color of cells will be covered by the new color. Since the magic of the brush is limited, each row and each column can only be painted at most once. The cells were painted in some other color (neither black nor white) initially.

    Please write a program to find out the way to paint the grid.

    Input

    There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

    The first line contains an integer N (1 <= N <= 500). Then N lines follow. Each line contains a string with N characters. Each character is either 'X' (black) or 'O' (white) indicates the color of the cells should be painted to, after Leo finished his painting.

    Output

    For each test case, output "No solution" if it is impossible to find a way to paint the grid.

    Otherwise, output the solution with minimum number of painting operations. Each operation is either "R#" (paint in a row) or "C#" (paint in a column), "#" is the index (1-based) of the row/column. Use exactly one space to separate each operation.

    Among all possible solutions, you should choose the lexicographically smallest one. A solution X is lexicographically smaller than Y if there exists an integer k, the first k - 1 operations of X and Y are the same. The k-th operation of X is smaller than the k-th in Y. The operation in a column is always smaller than the operation in a row. If two operations have the same type, the one with smaller index of row/column is the lexicographically smaller one.

    Sample Input

    2
    2
    XX
    OX
    2
    XO
    OX
    

    Sample Output

    R2 C1 R1
    No solution

      题意:在一个n*n的矩阵中,一开始什么也没有填,先告诉你可一次将一行全写成X,或者将一列全写成O。其中每一行每一列只可以写一次,给出一个目标状态,问最少多少步以后可以得到目标状态?如果有多个结果,按最少字典序输出,其中列优先,标号小的优先。
      经过分析,我们发现存在路径得出目标状态,那么一定有一行全是X或者一列全是O,那个如果我们在得出目标状态的时候只需要从上一个状态那里将这一行或者一列写了就可以了,上一个状态也同理,最终就可以推回去起始状态。其中每还原一次,那么在这一行(列)上面的符号就可以想成是随意了(既可以是O,也可以是X,还可以是什么也没有)。好像说得还不够清楚,详细还需要去看一下其他人的题解,相结合还看应该更好理解。
      做法:逆推+模拟,当然还需要小心的是有的时候逆推至前面的几步的时候会发现前几步是可以不走的,应该将其去掉。
      这里的代码写得比较恶心→_→,要喷请轻喷。

    上代码:

      1 #include <bits/stdc++.h>
      2 #define MAX 502
      3 using namespace std;
      4 
      5 int n;
      6 char cc[MAX][MAX];
      7 int X[MAX],O[MAX];
      8 bool r[MAX],c[MAX];
      9 vector<int> s;
     10 
     11 bool check(){
     12     int count=n<<1;
     13     while(count--){
     14         int u=5000;
     15         for(int i=n;i>=1;i--){
     16                 if(!r[i] && X[i]==n){
     17                     r[i]=1;
     18                     s.push_back(i+1000);
     19                     for(int j=1;j<=n;j++){
     20                         O[j]++;
     21                     }
     22                     u=i;
     23                     break;
     24                 }
     25         }
     26         if(u==5000){
     27             for(int i=n;i>=1;i--){
     28                 if(!c[i] && O[i]==n){
     29                  c[i]=1;
     30                  s.push_back(i);
     31                  for(int j=1;j<=n;j++){
     32                      X[j]++;
     33                  }
     34                  u=i;
     35                  break;
     36                 }
     37             }
     38         }
     39         if(u==5000) return 0;
     40     }
     41     return 1;
     42 }
     43 
     44 void reset(){
     45     s.clear();
     46     memset(X,0,sizeof(X));
     47     memset(O,0,sizeof(O));
     48     memset(r,0,sizeof(r));
     49     memset(c,0,sizeof(c));
     50 }
     51 
     52 int main()
     53 {
     54     int t;
     55     //freopen("data.txt","r",stdin);
     56     scanf("%d",&t);
     57     while(t--){
     58         reset();
     59         scanf("%d",&n);
     60         for(int i=1;i<=n;i++){
     61             scanf("%s",cc[i]+1);
     62             for(int j=1;j<=n;j++){
     63                 if(cc[i][j]=='X'){
     64                     X[i]++;
     65                 }else{
     66                     O[j]++;
     67                 }
     68             }
     69         }
     70         if(check()){
     71             int e=0;
     72             int u;
     73             memset(cc,0,sizeof(cc));
     74             int count=0;
     75             vector<int>::iterator i=s.begin();
     76             for(; i!=s.end();i++){
     77                 u=*i;
     78                 if(u<1000){
     79                     for(int j=1;j<=n;j++){
     80                         if(!cc[j][u]){
     81                             cc[j][u]=1;
     82                             count++;
     83                         }
     84                     }
     85                 }else{
     86                     u-=1000;
     87                     for(int j=1;j<=n;j++){
     88                         if(!cc[u][j]){
     89                             cc[u][j]=1;
     90                             count++;
     91                         }
     92                     }
     93                 }
     94                 if(count==n*n) break;
     95             }
     96             for(;i>s.begin()-1;i--){
     97                 u=*i;
     98                 if(e++) printf(" ");
     99                 if(u>1000)printf("R%d",u-1000);
    100                 else printf("C%d",u);
    101             }
    102             printf("
    ");
    103         }else{
    104             printf("No solution
    ");
    105         }
    106     }
    107     return 0;
    108 }
    3780
  • 相关阅读:
    vue路由学习
    vue组件学习
    Vue常用特性
    Vue入门常用指令
    ES6新增语法
    如何搭建一个vue项目(完整步骤)
    OA办公系统
    java有序数组的有序交集
    javascript输出数据到文件
    node js 实现文件上传与反显
  • 原文地址:https://www.cnblogs.com/sineatos/p/3689834.html
Copyright © 2011-2022 走看看