标签:cst bsp ips ges flag lin min text efi
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 16494 | Accepted: 6025 |
Description
Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an M × N grid (1 ≤ M ≤ 15; 1 ≤ N ≤ 15) of square tiles, each of which is colored black on one side and white on the other side.
As one would guess, when a single white tile is flipped, it changes to black; when a single black tile is flipped, it changes to white. The cows are rewarded when they flip the tiles so that each tile has the white side face up. However, the cows have rather large hooves and when they try to flip a certain tile, they also flip all the adjacent tiles (tiles that share a full edge with the flipped tile). Since the flips are tiring, the cows want to minimize the number of flips they have to make.
Help the cows determine the minimum number of flips required, and the locations to flip to achieve that minimum. If there are multiple ways to achieve the task with the minimum amount of flips, return the one with the least lexicographical ordering in the output when considered as a string. If the task is impossible, print one line with the word "IMPOSSIBLE".
Input
Output
Sample Input
4 4 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1
Sample Output
0 0 0 0 1 0 0 1 1 0 0 1 0 0 0 0
1 #include <cstdio> 2 #include <cctype> 3 #include <algorithm> 4 #include <cmath> 5 #include <cstring> 6 #define number s-‘0‘ 7 8 using namespace std; 9 10 const int MAX_N=16; 11 const int INF=0x3f3f3f3f; 12 const int dx[5]={-1, 0, 0, 0, 1}; 13 const int dy[5]={0, 1, 0, -1, 0}; 14 int N,M; 15 int flip[MAX_N][MAX_N], tile[MAX_N][MAX_N], opt[MAX_N][MAX_N]; 16 17 void read(int &x){ 18 char s; 19 x=0; 20 bool flag=0; 21 while(!isdigit(s=getchar())) 22 (s==‘-‘)&&(flag=true); 23 for(x=number;isdigit(s=getchar());x=x*10+number); 24 (flag)&&(x=-x); 25 } 26 27 void write(int x) 28 { 29 if(x<0) 30 { 31 putchar(‘-‘); 32 x=-x; 33 } 34 if(x>9) 35 write(x/10); 36 putchar(x%10+‘0‘); 37 } 38 39 int calc(); 40 int get(int, int); 41 42 int main() 43 { 44 read(M);read(N); 45 for (int i=0; i<M; i++) 46 for (int j=0; j<N; j++) 47 read(j[i[tile]]); 48 int res=-1; 49 for (int i=0; i< 1<<N; i++) 50 { 51 memset(flip, 0, sizeof(flip)); 52 for (int j=0; j<N; j++) 53 { 54 flip[0][N-j-1]=i>>j&1; 55 } 56 int num=calc(); 57 if (num>=0 && (res<0 || num<res)) 58 { 59 res=num; 60 memcpy(opt, flip, sizeof(flip)); 61 } 62 } 63 if (res<0) puts("IMPOSSIBLE\n"); 64 else 65 for (int i=0; i<M; i++) 66 for (int j=0; j<N; j++) 67 printf("%d%c", j[i[opt]], j+1==N? ‘\n‘: ‘ ‘); 68 } 69 70 int get(int x, int y) 71 { 72 int c=tile[x][y]; 73 for (int i=0; i<5; i++) 74 { 75 int x2=x+dx[i], y2=y+dy[i]; 76 if (0<=x2 && x2<M && 0<=y2 && y2<N) 77 { 78 c+=y2[x2[flip]]; 79 } 80 } 81 return c % 2; 82 } 83 84 int calc() 85 { 86 for (int i=1; i<M; i++) 87 { 88 for (int j=0; j<N; j++) 89 { 90 if (get(i-1,j)!=0) flip[i][j]=1; 91 } 92 } 93 for (int j=0; j<N; j++) 94 { 95 if (get(M-1,j)!=0) return -1; 96 } 97 int res=0; 98 for (int i=0; i<M; i++) 99 for (int j=0; j<N; j++) 100 res+=j[i[flip]]; 101 return res; 102 }
标签:cst bsp ips ges flag lin min text efi
原文地址:https://www.cnblogs.com/Ymir-TaoMee/p/9509503.html