SRM 550 DIV2
还是老样子,悲催,只做出来一个,第二个因为没有考虑一种情况,导致代码写错了
第一题就是简单的比较两个字符串中不同的就OK了,最后用数字差模2比较一下就行。
第二题以为可以在任意地方拐弯,实际上必须碰到边缘才可以,后来发现的时候没时间改代码了,悲催。
思路就是先比较边界的情况,如果合法的话时间上就是在一个矩形中绕圈,判断一下是否合法就OK了。
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <cstdlib> 5 #include <cmath> 6 #include <map> 7 #include <algorithm> 8 #include <list> 9 using namespace std; 10 11 12 class RotatingBot{ 13 public: 14 int minArea(vector <int> moves){ 15 int squ=0; 16 int left,top; 17 int x,y; 18 left=top=0; 19 int s=moves.size(); 20 if(s==1){ 21 return moves[0]+1; 22 }else if(2==s){ 23 return (moves[0]+1)*(moves[1]+1); 24 }else if(3==s){ 25 left = max(moves[0], moves[2]); 26 return (left+1)*(moves[1]+1); 27 }else if(4==s){ 28 if(moves[2]>moves[0]){ 29 left = max(moves[0], moves[2]); 30 top = max(moves[1],moves[3]); 31 return (left+1)*(top+1); 32 }else if(moves[2]==moves[0]){ 33 left = max(moves[0], moves[2]); 34 top = max(moves[1],moves[3]); 35 if(moves[3]<moves[1]) 36 return (left+1)*(top+1); 37 else 38 return -1; 39 }else{ 40 return -1; 41 } 42 }else{ 43 if (moves[2] > moves[0]) { 44 if (moves[3] > moves[1]) { 45 x = moves[2] ; 46 y = moves[3] - moves[1] - 1; 47 squ=(moves[3]+1)*(moves[2]+1); 48 } else if (moves[3] == moves[1]) { 49 x = moves[2] - moves[0] - 1; 50 y = moves[3] - 1; 51 squ=(moves[3]+1)*(moves[2]+1); 52 } else 53 return -1; 54 } else if (moves[2] == moves[0]) { 55 if (moves[3] == moves[1] - 1) { 56 x = moves[2] - 1; 57 y = moves[3] - 1; 58 squ=(moves[1]+1)*(moves[2]+1); 59 } else 60 return -1; 61 } else { 62 return -1; 63 } 64 } 65 int tag=0; 66 int i=4; 67 for(;i<moves.size()-1;i++){ 68 if(0==tag){ 69 if(moves[i]==x){ 70 71 }else{ 72 return -1; 73 } 74 }else{ 75 if(moves[i]==y){ 76 x--; 77 y--; 78 }else{ 79 return -1; 80 } 81 } 82 tag=(tag+1)%2; 83 } 84 if (0 == tag) { 85 if (moves[i] <= x) { 86 87 } else { 88 return -1; 89 } 90 } else { 91 if (moves[i] <= y) { 92 } else { 93 return -1; 94 } 95 } 96 return squ; 97 } 98 99 };
第三题是看了strongczq的代码才懂得,改了改写成了C++。
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <queue> 5 #include <set> 6 #include <algorithm> 7 #include <map> 8 using namespace std; 9 10 class TopView { 11 public: 12 string findOrder(vector<string> grid) { 13 set<char> colorSet; 14 for (int i = 0; i < grid.size(); ++i) { 15 for (int j = 0; j < grid[0].size(); ++j) { 16 char ch = grid[i][j]; 17 if (ch != '.') { 18 colorSet.insert(ch); 19 } 20 } 21 } 22 //grid包含的所有颜色 23 char* colors = new char[colorSet.size()]; 24 //颜色->索引值(colors数组的索引值)映射 25 int colorIndex[256]; 26 int cnt = 0; 27 set<char>::iterator itcolor; 28 for (itcolor = colorSet.begin(); colorSet.end() != itcolor; itcolor++) { 29 colorIndex[unsigned(*itcolor)] = cnt; 30 colors[cnt++] = *itcolor; 31 } 32 33 //卡片依赖关系,被依赖的必须先放置 34 map<int, map<int, bool> > dep; 35 for (int i = 0; i < cnt; i++) 36 for (int j = 0; j < cnt; j++) 37 dep[i][j] = false; 38 for (int i = 0; i < cnt; ++i) { 39 char ch = colors[i]; 40 int minR = 1000, maxR = -1, minC = 1000, maxC = -1; 41 for (int r = 0; r < grid.size(); ++r) { 42 for (int c = 0; c < grid[0].size(); ++c) { 43 if (grid[r][c] == ch) { 44 minR = min(minR, r); 45 maxR = max(maxR, r); 46 minC = min(minC, c); 47 maxC = max(maxC, c); 48 } 49 } 50 } 51 for (int r = minR; r <= maxR; ++r) { 52 for (int c = minC; c <= maxC; ++c) { 53 char ch2 = grid[r][c]; 54 if (ch2 == '.') { 55 return "ERROR!"; 56 } else if (ch2 == ch) { 57 continue; 58 } 59 int j = colorIndex[(unsigned int) ch2]; 60 dep[j][i] = true; 61 } 62 } 63 } 64 string res; 65 //每个卡片依赖的其他卡片数 66 int* depNum = new int[cnt]; 67 for (int i = 0; i < cnt; ++i) 68 depNum[i] = 0; 69 for (int i = 0; i < cnt; ++i) { 70 for (int j = 0; j < cnt; ++j) { 71 if (dep[i][j]) { 72 depNum[i]++; 73 } 74 } 75 } 76 //拓扑排序 77 for (int i = 0; i < cnt; ++i) { 78 int choice = 2550; 79 for (int j = 0; j < cnt; ++j) { 80 if (depNum[j] == 0) { 81 choice = min(choice, j); 82 } 83 } 84 85 if (choice == 2550) { 86 return "ERROR!"; 87 } 88 depNum[choice] = -1; 89 for (int j = 0; j < cnt; ++j) { 90 if (dep[j][choice]) { 91 depNum[j]--; 92 } 93 } 94 res = res + colors[choice]; 95 } 96 return res; 97 } 98 };