Zjuoj 3780 Paint the Grid Again

Source: Internet
Author: User

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780

Paint the Grid Again Time limit: 2 Seconds Memory Limit: 65536 KB

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

Leo have 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 is covered by the new color. Since the magic of the brush is limited, each row and each column can have only been painted at most once. The cells were painted in some and other color (neither black nor white) initially.

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

Input

There is multiple test cases. The first line of input contains an integer indicating the number of the T 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 are either ' X ' (black) or ' O ' (white) indicates the color of the cells should being painted to, after Leo Finis Hed his painting.

Output

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

Otherwise, output the solution with minimum number of painting operations. Each operation are 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 and is the X Y same. The k-th operation of are X smaller than the k-th in Y . The operation in a column are always smaller than the operation in a row. If the operations has the same type, the one with smaller index of Row/column is the lexicographically smaller one.

Sample Input
22xxox2xoox
Sample Output
R2 C1 r1no Solution

Author: YU, Xiaoyao
Source: The 11th Zhejiang Provincial Collegiate Programming Contest

Analysis

The matrix of a given n*n

There are 2 operations:

1. Turn the line into X

2. Turn a column into O

Limit: Each row (per column) can be changed only once

Given the result graph, at the beginning of the graph without o,x, ask the minimum number of steps (and the dictionary order is the smallest)

Ideas:

for (I,J) This lattice, if the present is O, then remove the row O, (let this row into X) can be directly considered (I,J) is X

So when a row of x is filled with n, you can remove the row x


Direct simulation can

First Preprocess all the rows and columns that are all o or X, and put them in a stack.

Because the dictionary order is minimal, the column is processed first, and column I is denoted by i+n, and the first row is denoted by i
Then the stack is ordered, so that the current situation is processed in the order, into a queue, and then one after another to remove it.

AC Code:

1#include <stdio.h>2#include <iostream>3#include <algorithm>4#include <string.h>5#include <math.h>6#include <vector>7#include <queue>8#include <Set>9 using namespacestd;Ten #defineN 1005 Onevector<int>ans; A CharMp[n][n]; - intN, H[n], l[n]; - intYes[n]; the intStack[n], Top; -  - voidinit () { - ans.clear (); +memset (yes,0,sizeofyes); -memset (H,0,sizeofh); +memset (L,0,sizeofl); ATop =0; at } - BOOLcmpintAintb) {returnA>b;} - //0-n-1 indicates that the column n-2n-1 represents the row - voidWork () { -Sort (Stack, stack+Top, CMP); -queue<int>Q; in     intI, J; -      for(inti =0; i < Top; i++){ toQ.push (Stack[i]), Ans.push_back (Stack[i]); yes[stack[i]]=-1; +     } -Top =0; the      while(!Q.empty ()) { *         intU =Q.front (); Q.pop (); $Top =0;Panax Notoginseng         if(u<N) -              for(j =0; J < N; J + +) the             { +Mp[j][u] ='X'; Ah[j]++; the                 if(yes[j+n]!=-1&& h[j]==n) stack[top++] = j+N; +             } -         Else { $u-=N; $              for(j =0; J < N; J + +) -             { -MP[U][J] ='O'; thel[j]++; -                 if(yes[j]!=-1&& l[j]==n) stack[top++] =J;Wuyi             } the         } -Sort (Stack, stack+Top, CMP); Wu              for(i =0; i < Top; i++) Q.push (Stack[i]), yes[stack[i]] =-1, Ans.push_back (Stack[i]); -     } About      for(inti =0; I <2*n; i++)if(yes[i]==0) {puts ("No Solution");return;} $      for(inti = ans.size ()-1; i>=0; i--){ -         intU =Ans[i]; -         if(u>=n) printf ("R"), u-=N; -         Elseprintf"C"); Aprintf"%d", u+1); +I? printf" "): Puts (""); the     } - } $ intMain () { the     intT;SCANF ("%d",&T); the     intI, J; the      while(t--){ thescanf"%d",&n); - init (); in          for(i=0; i<n;i++) scanf ("%s", Mp[i]); the          for(i=0; i<n;i++) the         { About              for(j =0; j<n; J + +)if(mp[i][j]=='X') h[i]++; the             if(h[i]==n) stack[top++] = i+N; the             Else if(h[i]==0) Yes[i+n] =-1; the         } +          for(i=0; i<n;i++) -         { the              for(j =0; j<n; J + +)if(mp[j][i]=='O') l[i]++;Bayi             if(l[i]==n) stack[top++] =i; the             Else if(l[i]==0) Yes[i] =-1; the         } -         if(top==0) {puts ("No Solution");Continue;} - Work (); the     } the     return 0; the } the /* -  About the 1 the O the 394 OOO the OOO the OOO the 98 2 About XX - OX101 2102 XO103 OX104  the 106 */
View Code

Zjuoj 3780 Paint the Grid Again

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.