Codeforces Round #419 (Div. 2)C. Karen and Game
Posted starry
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #419 (Div. 2)C. Karen and Game相关的知识,希望对你有一定的参考价值。
C. Karen and Game
给定n行m列数字,每次可以让一行或一列都减一,求出让全部数字全为0的最小的次数,没有则输出-1;
比赛时没有考虑,n和m的大小问题,被hack了。5555555555555555555555555555
好的方法没有想到,只有一个死方法了。
1 #include <iostream> 2 #include <stdio.h> 3 #define long long ll 4 using namespace std; 5 int a[110][110], n, m,b[110][110]; 6 bool check(){ 7 for(int i = 1; i <= n; i ++){ 8 for(int j = 1; j <= m; j ++){ 9 if(a[i][j] != 0){ 10 return true; 11 } 12 } 13 } 14 return false; 15 } 16 17 int main(){ 18 cin >> n >> m; 19 for(int i = 1; i <= n; i ++){ 20 for(int j = 1; j <= m; j ++){ 21 // scanf("%d",&a[i][j]); 22 cin >> a[i][j]; 23 b[i][j] = a[i][j]; 24 } 25 } 26 int sum, flag = 1, i = 1,j = 1, ans = 0; 27 if(n <= m){ 28 while(i <= n){ 29 sum = 0; 30 flag = 1; 31 for(int k = 1; k <= m; k ++){ 32 if(!a[i][k]){ 33 flag = 0; 34 break; 35 } 36 } 37 if(flag){ 38 ans++; 39 for(int k = 1; k <= m; k ++) a[i][k] -= 1; 40 }else i++; 41 } 42 while(j <= m){ 43 sum = 0; 44 flag = 1; 45 for(int k = 1; k <= n; k ++){ 46 if(!a[k][j]){ 47 flag = 0; 48 break; 49 } 50 } 51 if(flag){ 52 ans ++; 53 for(int k = 1; k <= n; k ++) a[k][j] -= 1; 54 }else j++; 55 } 56 }else{ 57 while(j <= m){ 58 sum = 0; 59 flag = 1; 60 for(int k = 1; k <= n; k ++){ 61 if(!a[k][j]){ 62 flag = 0; 63 break; 64 } 65 } 66 if(flag){ 67 ans ++; 68 for(int k = 1; k <= n; k ++) a[k][j] -= 1; 69 }else j++; 70 } 71 while(i <= n){ 72 sum = 0; 73 flag = 1; 74 for(int k = 1; k <= m; k ++){ 75 if(!a[i][k]){ 76 flag = 0; 77 break; 78 } 79 } 80 if(flag){ 81 ans++; 82 for(int k = 1; k <= m; k ++) a[i][k] -= 1; 83 }else i++; 84 } 85 } 86 if(check()){ 87 cout << -1 << endl; 88 return 0; 89 } 90 cout << ans << endl; 91 i = j = 1; 92 if(n <= m){ 93 while(i <= n){ 94 sum = 0; 95 flag = 1; 96 for(int k = 1; k <= m; k ++){ 97 if(!b[i][k]){ 98 flag = 0; 99 break; 100 } 101 } 102 if(flag){ 103 cout << "row " << i << endl; 104 for(int k = 1; k <= m; k ++) b[i][k] -= 1; 105 }else i++; 106 } 107 while(j <= m){ 108 sum = 0; 109 flag = 1; 110 for(int k = 1; k <= n; k ++){ 111 if(!b[k][j]){ 112 flag = 0; 113 break; 114 } 115 } 116 if(flag){ 117 cout << "col " << j << endl; 118 for(int k = 1; k <= n; k ++) b[k][j] -= 1; 119 }else j++; 120 } 121 }else{ 122 while(j <= m){ 123 sum = 0; 124 flag = 1; 125 for(int k = 1; k <= n; k ++){ 126 if(!b[k][j]){ 127 flag = 0; 128 break; 129 } 130 } 131 if(flag){ 132 cout << "col " << j << endl; 133 for(int k = 1; k <= n; k ++) b[k][j] -= 1; 134 }else j++; 135 } 136 while(i <= n){ 137 sum = 0; 138 flag = 1; 139 for(int k = 1; k <= m; k ++){ 140 if(!b[i][k]){ 141 flag = 0; 142 break; 143 } 144 } 145 if(flag){ 146 cout << "row " << i << endl; 147 for(int k = 1; k <= m; k ++) b[i][k] -= 1; 148 }else i++; 149 } 150 } 151 return 0; 152 }
以上是关于Codeforces Round #419 (Div. 2)C. Karen and Game的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #419 (Div. 1) (ABCDE)
Codeforces Round #419 (Div. 2) A-E