VJ:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=80667#overview
出处:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=65959#overview
越写到后面越是顺手,1次AC的越来越多,并且英文题目也读的越来越顺,算是一种进步吧。
A POJ 1321
穷举组合数dfs暴力,注意一个点确定那么那行那列就不能放
1 //组合数的回溯法 2 //用cstring或string.h 不要用memory.h 3 #include4 #include 5 using namespace std; 6 7 const int maxn = 100; 8 int n, k; 9 int px[maxn], py[maxn]; 10 int all; 11 int ans; 12 int visited[maxn]; 13 int vx[9], vy[9]; 14 15 void dfs(int num, int cur); 16 17 int main() 18 { 19 ios::sync_with_stdio(false); 20 while (cin >> n >> k) 21 { 22 if (n == -1 && k == -1)break; 23 ans = 0; 24 all = 0; 25 memset(px, 0, sizeof(px)); 26 memset(py, 0, sizeof(py)); 27 memset(visited, 0, sizeof(visited)); 28 char chTmp; 29 for (int i = 0; i < n; i++) 30 { 31 for (int j = 0; j < n; j++) 32 { 33 cin >> chTmp; 34 if (chTmp == '#') 35 { 36 px[all] = i; 37 py[all] = j; 38 all++; 39 } 40 } 41 cin.ignore(); 42 } 43 dfs(0, 0); 44 cout << ans << endl; 45 } 46 return 0; 47 } 48 49 void dfs(int num, int cur) 50 { 51 if (cur == k) 52 { 53 ans++; 54 } 55 else 56 { 57 for (int i = num; i < all; i++) 58 { 59 if (visited[i] == 0 && vx[px[i]] == 0 && vy[py[i]] == 0) 60 { 61 visited[i] = 1; 62 vx[px[i]]++; 63 vy[py[i]]++; 64 dfs(i + 1, cur + 1); 65 visited[i] = 0; 66 vx[px[i]]--; 67 vy[py[i]]--; 68 } 69 } 70 } 71 }
B POJ 2251
3DBFS,扩展而已。水题
1 #include2 #include<string> 3 #include 4 #include 5 using namespace std; 6 7 int L, R, C; 8 const int maxn = 32; 9 10 struct Node 11 { 12 int l, r, c; 13 int path; 14 //-1 = # 1 = . 2 = E 15 int own; 16 void assign(int x, int y, int z) 17 { 18 l = x; r = y; c = z; 19 path = -1; 20 own = 0; 21 } 22 }node[maxn][maxn][maxn]; 23 queue Q; 24 25 int bfs() 26 { 27 Node temp; 28 while (!Q.empty()) 29 { 30 temp = Q.front(); 31 if (temp.own == 2)return temp.path; 32 for (int dl = -1; dl < 2; dl++) 33 for (int dr = -1; dr < 2; dr++) 34 for (int dc = -1; dc < 2; dc++) 35 { 36 int a[3] = { dl, dr, dc }; 37 sort(a, a + 3); 38 if (!((a[0] == 0 && a[1] == 0) || (a[1] == 0 && a[2] == 0)))continue; 39 if (temp.l + dl > 0 && temp.l + dl <= L && temp.r + dr > 0 && temp.r + dr <= R && temp.c + dc > 0 && temp.c + dc <= C && node[temp.l + dl][temp.r + dr][temp.c + dc].own > 0 && node[temp.l + dl][temp.r + dr][temp.c + dc].path < 0) 40 { 41 node[temp.l + dl][temp.r + dr][temp.c + dc].path = node[temp.l][temp.r][temp.c].path + 1; 42 Q.push(node[temp.l + dl][temp.r + dr][temp.c + dc]); 43 } 44 } 45 Q.pop(); 46 } 47 return 0; 48 } 49 50 int main() 51 { 52 ios::sync_with_stdio(false); 53 while (cin >> L >> R >> C) 54 { 55 if (L == 0 && R == 0 && C == 0)break; 56 while (!Q.empty())Q.pop(); 57 string tmp; 58 for (int i = 1; i <= L; i++) 59 { 60 for (int j = 1; j <= R; j++) 61 { 62 cin >> tmp; 63 for (int k = 1; k <= C; k++) 64 { 65 node[i][j][k].assign(i, j, k); 66 switch (tmp[k - 1]) 67 { 68 case '.':node[i][j][k].own = 1; break; 69 case 'S':node[i][j][k].own = 1; node[i][j][k].path = 0; Q.push(node[i][j][k]); break; 70 case 'E':node[i][j][k].own = 2; break; 71 } 72 } 73 } 74 } 75 int ans = bfs(); 76 if (ans == 0)cout << "Trapped!\n"; 77 else cout << "Escaped in " << ans << " minute(s).\n"; 78 } 79 return 0; 80 }
C POJ 3278
隐式bfs。水题
1 #include2 #include 3 #include 4 using namespace std; 5 6 const int maxn = 100001; 7 8 int N, K; 9 queue<int> Q; 10 int path[maxn]; 11 12 int main() 13 { 14 cin >> N >> K; 15 memset(path, -1, sizeof(path)); 16 path[N] = 0; 17 Q.push(N); 18 int temp; 19 while (!Q.empty()) 20 { 21 temp = Q.front(); 22 Q.pop(); 23 if (temp == K) { cout << path[temp] << endl; } 24 if (temp > 0 && path[temp - 1] < 0) 25 { 26 Q.push(temp - 1); 27 path[temp - 1] = path[temp] + 1; 28 } 29 if (temp < 100000 && path[temp + 1] < 0) 30 { 31 Q.push(temp + 1); 32 path[temp + 1] = path[temp] + 1; 33 } 34 if (temp * 2 < maxn && path[temp * 2] < 0) 35 { 36 Q.push(temp * 2); 37 path[temp * 2] = path[temp] + 1; 38 } 39 } 40 return 0; 41 }
D POJ 3279
值得注意的是第一行一旦确定后面行也确定。从这点出发的暴力运算量就可以AC
1 //显然每个位置只能为0或1,不可能为2 2 //而当第一行一旦确定,后面的m-1行也就确定 3 //因此只要穷举第一行的情况,1<<16的运算量,再选字典序最小即可 4 #include5 #include 6 #include 7 using namespace std; 8 9 int M, N; 10 int m[17][17]; 11 int map[17][17]; 12 int ans[17][17]; 13 int minans[17][17]; 14 int minp = (1 << 30); 15 int minv = (1 << 30); 16 const int dx[] = { 1, -1, 0, 0 }; 17 const int dy[] = { 0, 0, 1, -1 }; 18 19 void flip(int row, int col) 20 { 21 if (map[row][col] == 0)map[row][col] = 1; 22 else map[row][col] = 0; 23 for (int i = 0; i < 4; i++) 24 { 25 if (row + dx[i] < 0 || row + dx[i] > M - 1 || col + dy[i] < 0 || col + dy[i] > N - 1)continue; 26 if (map[row + dx[i]][col + dy[i]] == 0)map[row + dx[i]][col + dy[i]] = 1; 27 else map[row + dx[i]][col + dy[i]] = 0; 28 } 29 } 30 31 int main() 32 { 33 cin >> M >> N; 34 memset(m, -1, sizeof(m)); 35 for (int i = 0; i < M; i++) 36 { 37 for (int j = 0; j < N; j++) 38 cin >> m[i][j]; 39 } 40 bool fuck = false; 41 for (int i = 0; i < (1 << N); i++) 42 { 43 memset(ans, 0, sizeof(ans)); 44 for (int x = 0; x < M; x++) 45 { 46 for (int y = 0; y < N; y++) 47 map[x][y] = m[x][y]; 48 } 49 for (int j = 0; j < N; j++) 50 { 51 ans[0][j] = (i & (1 << j)); 52 if (ans[0][j] > 0) 53 { 54 ans[0][j] = 1; 55 flip(0, j); 56 57 } 58 } 59 for (int j = 1; j < M; j++) 60 { 61 for (int k = 0; k < N; k++) 62 { 63 if (map[j - 1][k] == 1) 64 { 65 flip(j, k); 66 ans[j][k] = 1; 67 } 68 } 69 70 } 71 bool zz = true; 72 for (int j = 0; j < N; j++) 73 { 74 if (map[M - 1][j] == 1) 75 { 76 zz = false; 77 break; 78 } 79 } 80 if (zz) 81 { 82 fuck = true; 83 int p = 0; 84 for (int k = 0; k < M; k++) 85 for (int j = 0; j < N; j++) 86 { 87 p += ans[k][j]; 88 } 89 if (p < minv || (p == minv && minp > i)) 90 { 91 minv = p; 92 minp = i; 93 memcpy(minans, ans, sizeof(ans)); 94 } 95 } 96 } 97 if (!fuck)cout << "IMPOSSIBLE\n"; 98 else 99 { 100 for (int i = 0; i < M; i++) 101 { 102 for (int j = 0; j < N - 1; j++) 103 { 104 cout << minans[i][j] << " "; 105 } 106 cout << minans[i][N - 1] << endl; 107 } 108 } 109 return 0; 110 }
E POJ 1426
隐式bfs剪枝,从最高位1开始。利用数论的知识来优化就不会爆炸
1 //第一位必定为1 选最小的即可 2 //从最高位开始向后bfs,补0或1 3 //剪枝:对于完全剩余系0-n-1 4 //比如在第3位出现了k,而到第9位为0,结束 5 //那么我在第6位又出现k,显然到12位就会为0 6 //因此可以直接把又出现k的情况直接扔掉 7 #include8 #include 9 #include 10 using namespace std; 11 12 int N; 13 14 struct Node 15 { 16 int value; 17 int pos; 18 int store[205]; 19 Node() {} 20 Node(int a) :pos(a), value(1) { memset(store, 0, sizeof(store)); store[a] = 1; } 21 Node(Node n, int p) 22 { 23 pos = n.pos + 1; 24 value = (n.value * 10 + p) % N; 25 memcpy(store, n.store, sizeof(store)); 26 store[pos] = p; 27 } 28 void print() 29 { 30 for (int i = 1; i <= pos; i++) 31 { 32 cout << store[i]; 33 } 34 cout << endl; 35 } 36 }; 37 38 Node first; 39 queue Q; 40 int visited[205]; 41 42 int main() 43 { 44 while (cin >> N && N > 0) 45 { 46 memset(visited, 0, sizeof(visited)); 47 while (!Q.empty()) 48 { 49 Q.pop(); 50 } 51 first = Node(1); 52 Q.push(first); 53 while (!Q.empty()) 54 { 55 first = Q.front(); 56 Q.pop(); 57 if (visited[first.value] == 1)continue; 58 else visited[first.value] = 1; 59 if (first.value == 0) 60 { 61 first.print(); 62 break; 63 } 64 Q.push(Node(first, 0)); 65 Q.push(Node(first, 1)); 66 } 67 } 68 return 0; 69 }
F POJ 3126
注意素数的判别不要弄错,小于等于根号n
1 //注意判断素数那里小于等于根号i 2 #include3 #include<set> 4 #include 5 #include 6 #include 7 using namespace std; 8 9 int M; 10 bool prime[10005]; 11 int path[10005]; 12 queue<int> Q; 13 14 void primebuild() 15 { 16 memset(prime, 0, sizeof(prime)); 17 for (int i = 1001; i < 10000; i++) 18 { 19 int temp = floor(sqrt(1.0*i) + 0.5); 20 bool fuck = true; 21 for (int j = 2; j <= temp; j++) 22 { 23 if (i % j == 0) 24 { 25 fuck = false; 26 break; 27 } 28 } 29 prime[i] = fuck; 30 } 31 } 32 33 int bfs(int a, int b) 34 { 35 memset(path, -1, sizeof(path)); 36 while (!Q.empty()) 37 { 38 Q.pop(); 39 } 40 path[a] = 0; 41 Q.push(a); 42 int first; 43 while (!Q.empty()) 44 { 45 first = Q.front(); 46 Q.pop(); 47 if (first == b)return path[first]; 48 int p = first % 1000; 49 for (int i = 1; i < 10; i++) 50 { 51 p += 1000; 52 if (path[p] < 0 && prime[p]) 53 { 54 Q.push(p); 55 path[p] = path[first] + 1; 56 } 57 } 58 p = (first / 1000) * 1000 + (first % 100); 59 for (int i = 0; i < 10; i++) 60 { 61 if (path[p] < 0 && prime[p]) 62 { 63 Q.push(p); 64 path[p] = path[first] + 1; 65 } 66 p += 100; 67 } 68 p = first - (first % 100) + (first % 10); 69 for (int i = 0; i < 10; i++) 70 { 71 if (path[p] < 0 && prime[p]) 72 { 73 Q.push(p); 74 path[p] = path[first] + 1; 75 } 76 p += 10; 77 } 78 p = first - (first % 10); 79 for (int i = 0; i < 10; i++) 80 { 81 if (path[p] < 0 && prime[p]) 82 { 83 Q.push(p); 84 path[p] = path[first] + 1; 85 } 86 p++; 87 } 88 } 89 return -1; 90 } 91 92 int main() 93 { 94 primebuild(); 95 cin >> M; 96 while (M--) 97 { 98 int a, b; 99 cin >> a >> b; 100 cout << bfs(a, b) << endl; 101 } 102 return 0; 103 }
G POJ 3087
模拟题,注意某一状态第二次出现时剪枝。水题
1 //纯模拟题,注意两副牌重复交叉一定次数一定会循环到开始 2 #include3 #include 4 #include<string> 5 #include
H POJ 3414
细节上别弄错。bfs水题
1 //类的构造函数的形参类型为该类必须用引用 2 //注意set放结构体的时候必须要重载< 3 #include4 #include 5 #include<set> 6 #include<string> 7 using namespace std; 8 9 int a, b, c; 10 11 struct State 12 { 13 int la, lb; 14 bool ok() { if (la == c || lb == c)return true; return false; } 15 int path; 16 string way; 17 State() :path(-1), la(0), lb(0), way("") {} 18 }; 19 20 struct Node 21 { 22 int la, lb; 23 Node(State a):la(a.la), lb(a.lb) {} 24 }; 25 26 bool operator < (Node a, Node b) 27 { 28 if (a.la != b.la)return a.la < b.la; 29 else return a.lb < b.lb; 30 } 31 32 set S; 33 queue Q; 34 35 int main() 36 { 37 cin >> a >> b >> c; 38 State first; 39 first.path++; 40 Q.push(first); 41 bool fuck = false; 42 while (!Q.empty()) 43 { 44 first = Q.front(); 45 Q.pop(); 46 Node node(first); 47 if (S.count(node))continue; 48 else S.insert(node); 49 if (first.ok()) 50 { 51 cout << first.path << endl; 52 cout << first.way; 53 fuck = true; 54 break; 55 } 56 else 57 { 58 first.path++; 59 State temp = first; 60 if (first.la < a) 61 { 62 temp.way += "FILL(1)\n"; 63 temp.la = a; 64 Q.push(temp); 65 } 66 temp = first; 67 if (first.lb < b) 68 { 69 temp.way += "FILL(2)\n"; 70 temp.lb = b; 71 Q.push(temp); 72 } 73 temp = first; 74 if (first.la > 0) 75 { 76 temp.way += "DROP(1)\n"; 77 temp.la = 0; 78 Q.push(temp); 79 } 80 temp = first; 81 if (first.lb > 0) 82 { 83 temp.way += "DROP(2)\n"; 84 temp.lb = 0; 85 Q.push(temp); 86 } 87 temp = first; 88 if (first.la < a && first.lb > 0) 89 { 90 temp.way += "POUR(2,1)\n"; 91 int cc = a - first.la; 92 if (cc > first.lb) 93 { 94 temp.la += first.lb; 95 temp.lb = 0; 96 } 97 else 98 { 99 temp.lb -= cc; 100 temp.la = a; 101 } 102 Q.push(temp); 103 } 104 temp = first; 105 if (first.lb < b && first.la > 0) 106 { 107 temp.way += "POUR(1,2)\n"; 108 int cc = b - first.lb; 109 if (cc > first.la) 110 { 111 temp.lb += first.la; 112 temp.la = 0; 113 } 114 else 115 { 116 temp.la -= cc; 117 temp.lb = b; 118 } 119 Q.push(temp); 120 } 121 } 122 } 123 if (!fuck) cout << "impossible" << endl; 124 return 0; 125 }
I FZU 2150
dfs穷举组合数+bfs。注意笔误
1 //dfs穷举组合数+bfs暴力 2 #include3 #include 4 #include<set> 5 #include 6 #include 7 using namespace std; 8 9 int map[12][12]; 10 int grass[12][12]; 11 int T, n, m; 12 int px[105], py[105]; 13 int tot; 14 int fire[3]; 15 int ans; 16 int dx[] = { -1, 0, 1, 0 }; 17 int dy[] = { 0, 1, 0, -1 }; 18 int visited[12][12]; 19 20 struct Node 21 { 22 int x, y; 23 int path; 24 Node() {} 25 Node(int x, int y, int path) :x(x), y(y), path(path) {} 26 }; 27 28 queue Q; 29 30 void dfs(int cur, int num) 31 { 32 if (cur == 2) 33 { 34 memcpy(grass, map, sizeof(map)); 35 memset(visited, 0, sizeof(visited)); 36 int t = -1; 37 Node t1 = Node(px[fire[0]], py[fire[0]], 0), t2 = Node(px[fire[1]], py[fire[1]], 0); 38 Q.push(t1); 39 Q.push(t2); 40 while (!Q.empty()) 41 { 42 Node temp = Q.front(); 43 Q.pop(); 44 if (grass[temp.x][temp.y] == 0)continue; 45 else 46 { 47 t = max(t, temp.path); 48 grass[temp.x][temp.y] = 0; 49 for (int i = 0; i < 4; i++) 50 { 51 if (temp.x + dx[i] < 0 || temp.x + dx[i] > n - 1)continue; 52 if (temp.y + dy[i] < 0 || temp.y + dy[i] > m - 1)continue; 53 if (visited[temp.x + dx[i]][temp.y + dy[i]] == 1)continue; 54 Q.push(Node(temp.x + dx[i], temp.y + dy[i], temp.path + 1)); 55 visited[temp.x + dx[i]][temp.y + dy[i]] = 1; 56 } 57 } 58 } 59 bool fuck = true; 60 for (int i = 0; i < n; i++) 61 { 62 bool cc = true; 63 for (int j = 0; j < m; j++) 64 { 65 if (grass[i][j] == 1) 66 { 67 cc = false; 68 break; 69 } 70 } 71 if (!cc) 72 { 73 fuck = false; 74 break; 75 } 76 } 77 if (fuck) 78 { 79 ans = min(ans, t); 80 } 81 } 82 else 83 { 84 for (int i = num; i < tot; i++) 85 { 86 fire[cur] = i; 87 dfs(cur + 1, i); 88 } 89 } 90 } 91 92 int main() 93 { 94 ios::sync_with_stdio(false); 95 cin >> T; 96 for (int kase = 0; kase < T; kase++) 97 { 98 tot = 0; 99 ans = (1 << 30); 100 memset(map, 0, sizeof(map)); 101 memset(px, -1, sizeof(px)); 102 memset(py, -1, sizeof(py)); 103 cin >> n >> m; 104 char chTmp; 105 for (int i = 0; i < n; i++) 106 { 107 for (int j = 0; j < m; j++) 108 { 109 cin >> chTmp; 110 if (chTmp == '#') 111 { 112 map[i][j] = 1; 113 px[tot] = i; 114 py[tot] = j; 115 tot++; 116 } 117 else 118 map[i][j] = 0; 119 } 120 cin.ignore(); 121 } 122 dfs(0, 0); 123 if (ans == (1 << 30))ans = -1; 124 cout << "Case " << kase + 1 << ": " << ans << endl; 125 } 126 return 0; 127 }
J UVA 11624
注意状态的先后变化。bfs水题
1 #include2 #include 3 #include<string> 4 #include 5 using namespace std; 6 7 int T; 8 int R, C; 9 int fr, fc; 10 int map[1005][1005]; 11 int visited[1005][1005]; 12 const int dr[] = { 0, 1, 0, -1 }; 13 const int dc[] = { 1, 0, -1, 0 }; 14 15 struct Node 16 { 17 int row, col; 18 int path; 19 bool fire; 20 Node() {} 21 Node(int row, int col, int path, bool fire) :row(row), col(col), path(path), fire(fire) {} 22 }; 23 24 queue Q; 25 26 int main() 27 { 28 cin >> T; 29 while (T--) 30 { 31 while (!Q.empty()) 32 { 33 Q.pop(); 34 } 35 memset(map, -1, sizeof(map)); 36 memset(visited, 0, sizeof(visited)); 37 cin >> R >> C; 38 char chTmp; 39 for (int i = 1; i <= R; i++) 40 { 41 for (int j = 1; j <= C; j++) 42 { 43 cin >> chTmp; 44 if (chTmp == '#') 45 { 46 map[i][j] = -1; 47 } 48 else 49 { 50 if (chTmp == '.') 51 { 52 map[i][j] = 1; 53 } 54 else 55 { 56 if (chTmp == 'F') 57 { 58 map[i][j] = -2; 59 Q.push(Node(i, j, -1, true)); 60 } 61 else 62 { 63 map[i][j] = 1; 64 fr = i; 65 fc = j; 66 } 67 } 68 } 69 } 70 cin.ignore(); 71 } 72 Q.push(Node(fr, fc, 0, false)); 73 bool fuck = true; 74 Node first; 75 while (!Q.empty()) 76 { 77 first = Q.front(); 78 Q.pop(); 79 if (!first.fire) 80 { 81 for (int i = 0; i < 4; i++) 82 { 83 if (first.row + dr[i] < 1 || first.row + dr[i] > R || first.col + dc[i] < 1 || first.col + dc[i] > C) 84 { 85 fuck = false; 86 cout << first.path + 1 << endl; 87 break; 88 } 89 else 90 { 91 if (visited[first.row + dr[i]][first.col + dc[i]] == 0 && map[first.row + dr[i]][first.col + dc[i]] > 0) 92 { 93 visited[first.row + dr[i]][first.col + dc[i]] = 1; 94 Q.push(Node(first.row + dr[i], first.col + dc[i], first.path + 1, false)); 95 } 96 } 97 } 98 if (!fuck)break; 99 } 100 else 101 { 102 for (int i = 0; i < 4; i++) 103 { 104 if (first.row + dr[i] < 1 || first.row + dr[i] > R || first.col + dc[i] < 1 || first.col + dc[i] > C)continue; 105 if (map[first.row + dr[i]][first.col + dc[i]] > 0 && visited[first.row + dr[i]][first.col + dc[i]] == 0) 106 { 107 Q.push(Node(first.row + dr[i], first.col + dc[i], -1, true)); 108 map[first.row + dr[i]][first.col + dc[i]] = -2; 109 visited[first.row + dr[i]][first.col + dc[i]] = 1; 110 } 111 } 112 } 113 } 114 if (fuck)cout << "IMPOSSIBLE" << endl; 115 } 116 return 0; 117 }
K POJ 3984
本专题最简单的水题。
#include#include<string> #include using namespace std; int map[5][5]; int visited[5][5]; const int dr[] = { -1, 0, 1, 0 }; const int dc[] = { 0, -1, 0, 1 }; struct Node { int row, col; int path; string way; Node(int row, int col, int path) :row(row), col(col), path(path) { way = ""; } }; queue Q; int main() { for (int i = 0; i < 5; i++) for (int j = 0; j < 5; j++) cin >> map[i][j]; Node first = Node(0, 0, 0); first.way = "(0, 0)\n"; visited[0][0] = 1; Q.push(first); while (!Q.empty()) { first = Q.front(); Q.pop(); if (first.row == 4 && first.col == 4) { cout << first.way; break; } else { for (int i = 0; i < 4; i++) { if (first.row + dr[i] < 0 || first.row + dr[i] > 4 || first.col + dc[i] < 0 || first.col + dc[i] > 4) { continue; } else { if (map[first.row + dr[i]][first.col + dc[i]] == 0 && visited[first.row + dr[i]][first.col + dc[i]] == 0) { Node temp = first; temp.row += dr[i]; temp.col += dc[i]; temp.path++; temp.way += "("; temp.way += first.row + dr[i] + 48; temp.way += ", "; temp.way += first.col + dc[i] + 48; temp.way += ")\n"; Q.push(temp); visited[first.row + dr[i]][first.col + dc[i]] = 1; } } } } } return 0; }
L HDU 1241
紫书例题,八连通图。水题
1 #include2 #include 3 using namespace std; 4 5 const int maxn = 105; 6 const int dr[] = { -1, -1, -1, 0, 0, 1, 1, 1 }; 7 const int dc[] = { -1, 0, 1, -1, 1, -1, 0, 1 }; 8 9 int ans; 10 int r, c; 11 int map[maxn][maxn]; 12 int visited[maxn][maxn]; 13 void dfs(int row, int col); 14 15 int main() 16 { 17 char chTmp; 18 while (cin >> r >> c) 19 { 20 if (r == 0)break; 21 ans = 0; 22 memset(map, 0, sizeof(map)); 23 memset(visited, 0, sizeof(map)); 24 for (int i = 1; i <= r; i++) 25 { 26 for (int j = 1; j <= c; j++) 27 { 28 cin >> chTmp; 29 if (chTmp == '@') 30 map[i][j] = 1; 31 } 32 cin.ignore(); 33 } 34 for (int i = 1; i <= r; i++) 35 { 36 for (int j = 1; j <= c; j++) 37 { 38 if (map[i][j] == 1 && visited[i][j] == 0) 39 { 40 dfs(i, j); 41 ans++; 42 } 43 } 44 } 45 cout << ans << endl; 46 } 47 return 0; 48 } 49 50 void dfs(int row, int col) 51 { 52 visited[row][col] = 1; 53 for (int i = 0; i < 8; i++) 54 { 55 if (row + dr[i] < 1 || row + dr[i] > r || col + dc[i] < 1 || col + dc[i] > c)continue; 56 if (map[row + dr[i]][col + dc[i]] == 1 && visited[row + dr[i]][col + dc[i]] == 0) 57 { 58 dfs(row + dr[i], col + dc[i]); 59 } 60 } 61 }
M HDU 1495
和上面某题极其相似。水题。
1 #include2 #include<set> 3 #include 4 #include 5 using namespace std; 6 7 int s, n, m; 8 9 struct Node 10 { 11 int a, b, c; 12 int path; 13 Node(int a, int b, int c, int path) :a(a), b(b), c(c), path(path) {} 14 }; 15 16 struct State 17 { 18 int a, b, c; 19 State() {} 20 State(Node x) :a(x.a), b(x.b), c(x.c) {} 21 }; 22 23 bool operator < (const State& x, const State& y) 24 { 25 if (x.a != y.a) return x.a < y.a; 26 else 27 { 28 if (x.b != y.b) return x.b < y.b; 29 else return x.c < y.c; 30 } 31 } 32 33 set S; 34 queue Q; 35 36 int main() 37 { 38 while (cin >> s >> n >> m) 39 { 40 while (!Q.empty()) 41 { 42 Q.pop(); 43 } 44 S.clear(); 45 if (s == 0)break; 46 Node first = Node(s, 0, 0, 0); 47 State temp; 48 Q.push(first); 49 bool fuck = true; 50 if (s % 2 == 0) 51 while (!Q.empty()) 52 { 53 first = Q.front(); Q.pop(); 54 temp = State(first); 55 int pp[3] = { first.a, first.b, first.c }; 56 sort(pp, pp + 3); 57 if (pp[1] == s / 2 && pp[2] == s / 2) 58 { 59 cout << first.path << endl; 60 fuck = false; 61 break; 62 } 63 if (S.count(temp) == 1) 64 { 65 continue; 66 } 67 else 68 { 69 S.insert(temp); 70 if (first.a > 0) 71 { 72 if (first.b < n) 73 { 74 Q.push(Node(max(0, first.a - n + first.b), min(n, first.b + first.a), first.c, first.path + 1)); 75 } 76 if (first.c < n) 77 { 78 Q.push(Node(max(0, first.a - m + first.c), first.b, min(m, first.c + first.a), first.path + 1)); 79 } 80 } 81 if (first.b > 0) 82 { 83 Q.push(Node(first.a + first.b, 0, first.c, first.path + 1)); 84 if (first.c < m) 85 { 86 Q.push(Node(first.a, max(0, first.b - m + first.c), min(m, first.c + first.b), first.path + 1)); 87 } 88 } 89 if (first.c > 0) 90 { 91 Q.push(Node(first.a + first.c, first.b, 0, first.path + 1)); 92 if (first.b < m) 93 { 94 Q.push(Node(first.a, min(n, first.b + first.c), max(0, first.c - n + first.b), first.path + 1)); 95 } 96 } 97 } 98 } 99 if (fuck)cout << "NO" << endl; 100 } 101 return 0; 102 }
N HDU 2612
注意不能用太暴力的方法,不然会MLE/TLE
1 //一开始的思路是穷举每个点到Y M的距离, TLE 2 //改进了一下,打表,两次BFS求Y\M到每个点的距离,AC 3 #include4 #include<string> 5 #include 6 #include 7 #include 8 #include 9 using namespace std; 10 11 const int maxn = 205; 12 const int dr[] = { -1, 0, 1, 0 }; 13 const int dc[] = { 0, -1, 0, 1 }; 14 int r, c; 15 int map[maxn][maxn]; 16 int visited[maxn][maxn]; 17 int tot = 0; 18 int kr[maxn*maxn], kc[maxn*maxn]; 19 int yr, yc, mr, mc; 20 int yk[maxn*maxn], mk[maxn*maxn]; 21 22 struct Node 23 { 24 int row, col; 25 int path; 26 Node() {} 27 Node(int row, int col, int path) :row(row), col(col), path(path) {} 28 }; 29 queue Q; 30 31 int main() 32 { 33 char chTmp; 34 while (scanf("%d %d\n", &r, &c) != EOF) 35 { 36 memset(map, -1, sizeof(map)); 37 for (int i = 1; i <= r; i++) 38 { 39 for (int j = 1; j <= c; j++) 40 { 41 cin >> chTmp; 42 if (chTmp == '.') 43 { 44 map[i][j] = 0; 45 } 46 else 47 { 48 if (chTmp == '@') 49 { 50 map[i][j] = ++tot; 51 kr[tot] = i; 52 kc[tot] = j; 53 } 54 else 55 { 56 if (chTmp == 'Y') 57 { 58 map[i][j] = 0; 59 yr = i; 60 yc = j; 61 } 62 else 63 { 64 if (chTmp == 'M') 65 { 66 map[i][j] = 0; 67 mr = i; 68 mc = j; 69 } 70 } 71 } 72 } 73 } 74 getchar(); 75 } 76 Node temp; 77 memset(visited, 0, sizeof(visited)); 78 memset(yk, -1, sizeof(yk)); 79 Q.push(Node(yr, yc, 0)); 80 visited[yr][yc] = 1; 81 while (!Q.empty()) 82 { 83 temp = Q.front(); Q.pop(); 84 if (map[temp.row][temp.col] > 0) 85 { 86 yk[map[temp.row][temp.col]] = temp.path; 87 } 88 for (int i = 0; i < 4; i++) 89 { 90 if (temp.row + dr[i] < 1 || temp.row + dr[i] > r || temp.col + dc[i] < 1 || temp.col + dc[i] > c)continue; 91 if (visited[temp.row + dr[i]][temp.col + dc[i]] == 0 && map[temp.row + dr[i]][temp.col + dc[i]] >= 0) 92 { 93 visited[temp.row + dr[i]][temp.col + dc[i]] = 1; 94 Q.push(Node(temp.row + dr[i], temp.col + dc[i], temp.path + 1)); 95 } 96 } 97 } 98 memset(visited, 0, sizeof(visited)); 99 memset(mk, -1, sizeof(mk)); 100 Q.push(Node(mr, mc, 0)); 101 visited[mr][mc] = 1; 102 while (!Q.empty()) 103 { 104 temp = Q.front(); Q.pop(); 105 if (map[temp.row][temp.col] > 0) 106 { 107 mk[map[temp.row][temp.col]] = temp.path; 108 } 109 for (int i = 0; i < 4; i++) 110 { 111 if (temp.row + dr[i] < 1 || temp.row + dr[i] > r || temp.col + dc[i] < 1 || temp.col + dc[i] > c)continue; 112 if (visited[temp.row + dr[i]][temp.col + dc[i]] == 0 && map[temp.row + dr[i]][temp.col + dc[i]] >= 0) 113 { 114 visited[temp.row + dr[i]][temp.col + dc[i]] = 1; 115 Q.push(Node(temp.row + dr[i], temp.col + dc[i], temp.path + 1)); 116 } 117 } 118 } 119 int ans = (1 << 30); 120 for (int i = 1; i <= tot; i++) 121 { 122 if (yk[i] == -1 || mk[i] == -1)continue; 123 ans = min(ans, yk[i] + mk[i]); 124 } 125 printf("%d\n", ans * 11); 126 } 127 return 0; 128 }