题意:给你一个网格图 ,每个网格中是W,B,两种颜色和 ? 表示未知,相同颜色的网格不能相邻,问你能否满足条件
解题思路:W和B的位置 和 (i+j) 的奇偶有关。
解题代码:
1 // BEGIN CUT HERE 2 /* 3 4 */ 5 // END CUT HERE 6 #line 7 "BichromeBoard.cpp" 7 #include <cstdlib> 8 #include <cctype> 9 #include <cstring> 10 #include <cstdio> 11 #include <cmath> 12 #include <algorithm> 13 #include <vector> 14 #include <string> 15 #include <iostream> 16 #include <sstream> 17 #include <map> 18 #include <set> 19 #include <queue> 20 #include <stack> 21 #include <fstream> 22 #include <numeric> 23 #include <iomanip> 24 #include <bitset> 25 #include <list> 26 #include <stdexcept> 27 #include <functional> 28 #include <utility> 29 #include <ctime> 30 using namespace std; 31 32 #define PB push_back 33 #define MP make_pair 34 35 #define REP(i,n) for(i=0;i<(n);++i) 36 #define FOR(i,l,h) for(i=(l);i<=(h);++i) 37 #define FORD(i,h,l) for(i=(h);i>=(l);--i) 38 39 typedef vector<int> VI; 40 typedef vector<string> VS; 41 typedef vector<double> VD; 42 typedef long long LL; 43 typedef pair<int,int> PII; 44 45 int ok = 0 ; 46 47 class BichromeBoard 48 { 49 public: 50 string ableToDraw(vector <string> str) 51 { 52 int n = str.size(); 53 int m = str[0].size(); 54 int st = 0 ; 55 int ok = 1 ; 56 for(int i = 0 ;i < n ; i ++) 57 for(int j = 0 ;j < m;j ++) 58 { 59 if(str[i][j] != ‘?‘){ 60 if(str[i][j] == ‘B‘) 61 { 62 st = (i + j) % 2; 63 for(int i = 0;i < n;i ++) 64 { 65 for(int j = 0 ;j < m;j ++) 66 { 67 if(str[i][j] == ‘W‘ && (i + j) % 2 == st) 68 ok = 0 ; 69 if(str[i][j] == ‘B‘ && (i + j) % 2 != st) 70 ok = 0 ; 71 } 72 } 73 }else{ 74 st = (i + j) % 2; 75 for(int i = 0;i < n;i ++) 76 { 77 for(int j = 0 ;j < m;j ++) 78 { 79 if(str[i][j] == ‘B‘ && (i + j) % 2 == st) 80 ok = 0 ; 81 if(str[i][j] == ‘W‘ && (i + j) % 2 != st) 82 ok = 0 ; 83 } 84 } 85 } 86 break; 87 } 88 } 89 if(ok) 90 return "Possible"; 91 else return "Impossible"; 92 } 93 94 // BEGIN CUT HERE 95 public: 96 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); } 97 private: 98 template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << ‘\"‘ << *iter << "\","; os << " }"; return os.str(); } 99 void verify_case(int Case, const string &Expected, const string &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << ‘\"‘ << endl; cerr << "\tReceived: \"" << Received << ‘\"‘ << endl; } } 100 void test_case_0() { string Arr0[] = {"W?W", 101 "??B", 102 "???"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "Possible"; verify_case(0, Arg1, ableToDraw(Arg0)); } 103 void test_case_1() { string Arr0[] = {"W??W"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "Impossible"; verify_case(1, Arg1, ableToDraw(Arg0)); } 104 void test_case_2() { string Arr0[] = {"??"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "Possible"; verify_case(2, Arg1, ableToDraw(Arg0)); } 105 void test_case_3() { string Arr0[] = {"W???", 106 "??B?", 107 "W???", 108 "???W"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "Possible"; verify_case(3, Arg1, ableToDraw(Arg0)); } 109 void test_case_4() { string Arr0[] = {"W???", 110 "??B?", 111 "W???", 112 "?B?W"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "Impossible"; verify_case(4, Arg1, ableToDraw(Arg0)); } 113 void test_case_5() { string Arr0[] = {"B"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "Possible"; verify_case(5, Arg1, ableToDraw(Arg0)); } 114 115 // END CUT HERE 116 117 }; 118 119 // BEGIN CUT HERE 120 int main() 121 { 122 BichromeBoard ___test; 123 ___test.run_test(-1); 124 return 0; 125 } 126 // END CUT HERE
时间: 2024-11-10 19:48:32