1001(简单题)
输入多组数据,每组数据为整数a,b;要求输出数据a+b;
1 int main() { 2 int a, b; 3 while (cin >> a >> b) { 4 cout << a + b << endl; 5 } 6 system("pause"); 7 return 0; 8 }
1002(遍历)
问题重述:给出一个不超过4*4的矩阵,里面有已经存在的墙,现在,你要放置士兵在里面,一个位置放一个,那么4*4,可以放16个,其中放置的规则就是,士兵的上下左右,不能再碰见一样的士兵,也就是两个士兵不能在一起,目的,求解出一个棋盘里面能放置的最多的士兵。
考点:矩阵遍历,但是这个不能简单的从(0,0)开始放置,而是通过队的思想,通过取余循环所以初始放置点。
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstdlib> 4 #include<string> 5 #include<string.h> 6 #include<algorithm> 7 #include<stack> 8 #include<math.h> 9 #include<queue> 10 #include<vector> 11 #include<map> 12 #include<set> 13 using namespace std; 14 int main() { 15 int n; 16 while (cin >> n) { 17 if (!n) 18 break; 19 int *tep = new int[n*n]; 20 char w; 21 for (int i = 0; i < n*n; i++) 22 { 23 cin >> w; 24 if (w == 'X') 25 tep[i] = 1; 26 else 27 tep[i] = 0; 28 } 29 int *p = new int[n*n]; 30 for (int i = 0; i < n*n; i++) 31 p[i] = tep[i]; 32 int sum = 0; 33 for (int z = 0; z < n*n; z++) { 34 for (int i = 0; i < n*n; i++) 35 tep[i] = p[i]; 36 int i = z; 37 int count = 0; 38 while (1) { 39 if (tep[i] == 0) { 40 int x = i / n; 41 int y = i % n; 42 bool falg = true; 43 while (falg&&--y >= 0) { 44 if (tep[x*n + y] == -1) 45 { 46 falg = false; 47 break; 48 } 49 else if (tep[x*n + y] == 1) { 50 break; 51 } 52 } 53 x = i / n; y = i % n; 54 while (falg&&++y < n) { 55 if (tep[x*n + y] == -1) 56 { 57 falg = false; 58 break; 59 } 60 else if (tep[x*n + y] == 1) { 61 break; 62 } 63 } 64 x = i / n; y = i % n; 65 while (falg&&++x < n) { 66 if (tep[x*n + y] == -1) 67 { 68 falg = false; 69 break; 70 } 71 else if (tep[x*n + y] == 1) { 72 break; 73 } 74 } 75 x = i / n; y = i % n; 76 while (falg&&--x >= 0) { 77 if (tep[x*n + y] == -1) 78 { 79 falg = false; 80 break; 81 } 82 else if (tep[x*n + y] == 1) { 83 break; 84 } 85 } 86 if (falg) 87 { 88 count++; 89 tep[i] = -1; 90 } 91 } 92 i = (i + 1) % (n*n); 93 if (i == z) 94 break; 95 } 96 if (count > sum) 97 sum = count; 98 } 99 cout << sum << endl; 100 delete[]tep; 101 } 102 system("pause"); 103 return 0; 104 }
1004(递归)
问题重述:给出两个字符串,请问第一个字符串能否通过栈的出入最后转换为第二个字符串,如果能,打印出全部的出入信息,用i代表入栈用o代表出栈,
例如:madam adamm 那么其中之一的解决方案就是:i i i i o o o i o o
考点:栈的所有出入情况遍历算法,采用深度遍历,找出所有情况,特殊点(出栈的时候,判断栈顶的字符,是否对应上了目标字符,然后最后通过出栈,出栈次数是否满足规定来判断是否达到目标)!
#include<iostream> #include<cmath> #include<cstdio> #include<algorithm> #include<cstring> #include<string> #include<queue> #include<stack> using namespace std; string s1; string s2; int len; stack<char> p; vector<char> ans; void dfs(int ipush, int ipop) { if (ipush == len && ipop == len) { for (int i = 0; i < ans.size(); i++) cout << ans[i]<<" "; cout << endl; return; } if (ipush < len) { p.push(s1[ipush]); ans.push_back('i'); dfs(ipush + 1, ipop); p.pop(); ans.pop_back(); } if (ipop < len&&p.size()>0 && p.top() == s2[ipop]) { char w = p.top(); p.pop(); ans.push_back('o'); dfs(ipush, ipop + 1); p.push(w); ans.pop_back(); } } int main() { while (cin >> s1 >> s2) { cout << "[" << endl; len = s1.length(); if (s2.length() != len) { cout << "]" << endl; continue; } dfs(0, 0); cout << "]" << endl; } system("pause"); return 0; }
1005(递归)
问题重述:给出两个瓶子,以及两个瓶子的容积,再给出一个目标体积,让你通过两个瓶子,量出我们需要的体积,比如说,给你两个瓶子,一个是3L,另外一个是5L,让你量出4L水,给出具体的操作方案;
例如 3 5 4;
答案:fill B;pour B A ;empty A ;pour B A ;fill B ;pour B A ;success;
思路:通过递归找出所有的解,然后判满足条件,退出,(注意,这里可能有多种方案)
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstdlib> 4 #include<string> 5 #include<string.h> 6 #include<algorithm> 7 #include<stack> 8 #include<math.h> 9 #include<queue> 10 #include<vector> 11 #include<map> 12 #include<set> 13 using namespace std; 14 int len;//记录第几层就找到解,判断最短解;! 15 vector<string> ans; //存放答案; 16 int a1, b1,aim; 17 set<int> tt; 18 void dfs(int a2, int b2, vector<string> tep) { 19 int x = a2, y = b2; 20 vector<string> tep1 = tep; 21 if (a2 == 0) { 22 tep.push_back("fill A"); 23 a2 = a1; 24 tep.push_back("pour A B"); 25 b2 = b2 + a2; //b中残留的加上新来的; 26 a2 = b2 - b1; //a中还有多少; 27 if (a2 < 0) //b还没有装满; 28 a2 = 0; 29 else 30 { 31 b2 = 0; 32 tep.push_back("empty B"); 33 } 34 if (a2 == 0 && b2 == 0) //都为空就是无效的! 35 return; 36 if (a2 == aim || b2 == aim) { //判断是否达到目标! 37 tep.pop_back(); 38 tep.push_back("success"); 39 if (tep.size() < len) { 40 ans = tep; 41 len = ans.size(); 42 return; 43 } 44 } 45 else if (a2 == 0 && tt.find(b2) == tt.end()) { 46 tt.insert(b2); 47 } 48 else if (b2 == 0 && tt.find(a2) == tt.end()) { 49 tt.insert(a2); 50 } 51 else { 52 return; 53 } 54 dfs(a2, b2, tep); 55 //到现在,不知道到底哪个瓶子是空的,只能交换, 56 swap(a2, b2); 57 if (b2 == 0) 58 tep.push_back("pour B A");//那么这里就有问题;! 59 else if (a2 == 0) 60 tep.push_back("pour A B");//那么这里就有问题;! 61 if (a2 < a1&&b2 < b1) { 62 dfs(a2, b2, tep); //交换后,没有溢出,之间下一层; 63 } 64 else if (a2 >= a1) { //交换后 ,出现问题;产是新的数据,而且新的数据,肯定都小于两个瓶子的容积,所以可以之间交换; 65 b2 = a2 - a1; 66 a2 = 0; 67 if (b2 != 0) { 68 tep.push_back("empty A"); 69 dfs(a2, b2, tep); 70 dfs(b2, a2, tep); 71 } 72 } 73 else if (b2 >= b1) { 74 a2 = b2 - b1; 75 b2 = 0; 76 if (a2 != 0) { 77 tep.push_back("empty B"); 78 dfs(a2, b2, tep); 79 dfs(b2, a2, tep); 80 } 81 } 82 } 83 //交换的过程; 84 tep = tep1; 85 if (y == 0) { 86 tep.push_back("fill B"); 87 y = b1; 88 tep.push_back("pour B A"); 89 x = x + y; 90 y = x - a1; 91 if (y < 0) 92 y = 0; 93 else { 94 x = 0; 95 tep.push_back("empty A"); 96 } 97 if (x == 0 && y == 0) 98 return; 99 if (x == aim || y == aim) { 100 tep.pop_back(); 101 tep.push_back("success"); 102 if (tep.size() < len) { 103 ans = tep; 104 len = ans.size(); 105 return; 106 } 107 } 108 else if (x == 0 && tt.find(y) == tt.end()) { 109 tt.insert(y); 110 } 111 else if (y == 0 && tt.find(x) == tt.end()) { 112 tt.insert(x); 113 } 114 else { 115 return; 116 } 117 dfs(x, y, tep); 118 swap(x, y); 119 if (x == 0) 120 tep.push_back("pour A B"); 121 else if (y == 0) 122 tep.push_back("pour B A"); 123 if (x < a1&&y < b1) { 124 dfs(x, y, tep); 125 } 126 else if (x >= a1) { 127 y = x - a1; 128 x = 0; 129 tep.push_back("empty A"); 130 if (y != 0) { 131 dfs(x, y, tep); 132 dfs(y, x, tep); 133 } 134 } 135 else if (y >= b1) { 136 x = y - b1; 137 y = 0; 138 tep.push_back("empty B"); 139 if (x != 0) { 140 dfs(x, y, tep); 141 dfs(y, x, tep); 142 } 143 } 144 } 145 } 146 int main() { 147 while (cin >> a1 >> b1 >> aim) { 148 len = 10000; 149 vector<string> p; 150 dfs(0, 0, p); 151 for (int i = 0; i < ans.size(); i++) { 152 cout << ans[i] << endl; 153 } 154 } 155 system("pause"); 156 return 0; 157 }
1008 (递归-分类讨论)
问题重述:给出一个方阵,每一个小组里面也是一个正方形,按对角线切开,然后给四个区域(上,下,左,右)赋予数值,给出的元素是随机的,目的就是,你重新排版这个方阵,让相邻的元素的值相同,判断存不存在解决方案!
思路:从左上角,从左到右,从上到下,递归遍历,判断左上角(左,上)是不是和已经存在的元素值相同,再判断(右,下)是否和剩下未参加排版的元素可能存在的组合,如果前驱后继都满足,则放入,不断放入,最后剩下一个的时候,就能判断是否有解!但是如果5*5,那么所有的排序方式就存在25!的阶乘,值太大,计算机耗时,虽然我在基础上改进了一点,但是目测任然还是阶乘。
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstdlib> 4 #include<string> 5 #include<string.h> 6 #include<algorithm> 7 #include<stack> 8 #include<math.h> 9 #include<queue> 10 #include<vector> 11 #include<map> 12 #include<set> 13 using namespace std; 14 int n; 15 int *v; 16 int *t; 17 class node { 18 public: 19 int index; 20 int x, y, l, r; 21 node() {} 22 }; 23 node *p; 24 bool dg(int in) { 25 bool falg = false; 26 int x = in / n; 27 int y = in % n; 28 for (int i = 0; i < n*n; i++) { 29 if (v[i] != 0) 30 continue; 31 bool xx = false, yy = false; 32 if (x != 0&&y!=0) { 33 if ((p[i].l == p[t[x*n + y - 1]].r) && (p[i].x == p[t[(x - 1)*n + y]].y)) { //该层次是做上角与已经存在的对应;这个肯定是存在的,因为上层已经遍历过了, 34 //第二次遍历,找到有接下来的点; 这个是找将来是否有满足条件的!;---------这个可能会有不存在的解,如果不存在就要退------------出循环! 35 if (y == n - 1 && x == n - 1) { 36 t[in] = i; 37 return true; //-------------------------------结果! 38 } 39 else if (y == n - 1) { 40 yy = true; 41 for (int j = 0; j < n*n; j++) { 42 if (v[j] != 0 || i == j) 43 continue; 44 if (p[j].x == p[i].y) { //!已经存在,满足条件!; 45 xx = true; 46 break; 47 } 48 } 49 } 50 else if (x == n - 1) { 51 xx = true; 52 for (int j = 0; j < n*n; j++) { 53 if (v[j] != 0 || i == j) 54 continue; 55 if (p[j].l == p[i].r) { //!已经存在,满足条件!; 56 yy = true; 57 break; 58 } 59 } 60 } 61 else { 62 for (int j = 0; j < n*n; j++) { 63 if (v[j] != 0 || i == j) 64 continue; 65 if (p[j].x == p[i].y) 66 xx = true; 67 if (p[j].l == p[i].r) 68 yy = true; 69 if (xx&&yy) 70 break; 71 } 72 } 73 } 74 } 75 else if (x == 0 && y != 0) { 76 if (p[i].l == p[t[x*n + y - 1]].r) { 77 //第二次遍历,找到有接下来的点; 78 if (y == n - 1) { 79 yy = true; 80 for (int j = 0; j < n*n; j++) { 81 if (v[j] != 0||i==j) 82 continue; 83 if (p[j].x == p[i].y) { //!已经存在,满足条件!; 84 xx = true; 85 break; 86 } 87 } 88 } 89 else { 90 for (int j = 0; j < n*n; j++) { 91 if (v[j] != 0 || i == j) 92 continue; 93 if (p[j].x == p[i].y) 94 xx = true; 95 if (p[j].l == p[i].r) 96 yy = true; 97 if (xx&&yy) 98 break; 99 } 100 } 101 } 102 } 103 else if (x != 0 && y == 0) { 104 if (p[i].x == p[t[(x - 1)*n + y]].y) { 105 //第二次遍历,找到有接下来的点; 106 if (x == n - 1) { 107 xx = true; 108 for (int j = 0; j < n*n; j++) { 109 if (v[j] != 0 || i == j) 110 continue; 111 if (p[j].l == p[i].r) { //!已经存在,满足条件!; 112 yy = true; 113 break; 114 } 115 } 116 } 117 else { 118 for (int j = 0; j < n*n; j++) { 119 if (v[j] != 0 || i == j) 120 continue; 121 if (p[j].x == p[i].y) 122 xx = true; 123 if (p[j].l == p[i].r) 124 yy = true; 125 if (xx&&yy) 126 break; 127 } 128 } 129 } 130 } 131 else if(x==0&&y==0){ 132 for (int j = 0; j < n*n; j++) { 133 if (v[j] != 0 || i == j) 134 continue; 135 if (p[j].x == p[i].y) 136 xx = true; 137 if (p[j].l == p[i].r) 138 yy = true; 139 if (xx&&yy) 140 break; 141 } 142 } 143 //---------找到满足条件的点,进入下一层递归中! 144 if (xx&&yy) 145 { 146 v[i] = 1; 147 t[in] = i; 148 falg=dg(in + 1); 149 if(!falg) { 150 v[i] = 0; 151 } 152 } 153 if (falg) 154 break; 155 } 156 return falg; 157 } 158 int main() { 159 int j = 0; 160 cin >> n; 161 while (n!=0) { 162 p = new node[n*n]; 163 v = new int[n*n]; 164 t = new int[n*n]; 165 fill(v, v + n * n, 0); 166 for (int i = 0; i < n*n; i++) 167 cin >> p[i].x >> p[i].r >> p[i].y >> p[i].l; 168 if (dg(0)) { 169 cout << "Game " << ++j << ":Possible" << endl; 170 } 171 else { 172 cout << "Game " << ++j << ":Impossible" << endl; 173 } 174 cin >> n; 175 if(n!=0) 176 cout << endl; 177 } 178 system("pause"); 179 return 0; 180 }