TOJ:Prairie dogs

来源:互联网 发布:sybase数据库入门 编辑:程序博客网 时间:2024/05/02 08:27

2192.   Prairie dogs I

#include <iostream>#include <cstdio>#include <cstring>#define MAXN 10using namespace std;char s[MAXN][MAXN];bool v[MAXN][MAXN];int m, n, room, cirs, area;void search(int x, int y){if (x >= 0 && y >= 0 && x < m && y < n && s[x][y] == 'X'){s[x][y] = '*'; area++;search(x, y + 1); search(x + 1, y + 1);search(x + 1, y); search(x + 1, y - 1);search(x - 1, y); search(x - 1, y - 1);search(x, y - 1); search(x - 1, y + 1);}}void cal(int x, int y){if (x >= 0 && x < m && y >= 0 && y < n && s[x][y] == '*'){if (x == 0 || x == m - 1) cirs++;if (y == 0 || y == n - 1) cirs++;s[x][y] = '#'; v[x][y] = 1;if (x > 0 && s[x - 1][y] != '*' && !v[x - 1][y]) cirs++;if (x < m - 1 && s[x + 1][y] != '*' && !v[x + 1][y]) cirs++;if (y > 0 && s[x][y - 1] != '*' && !v[x][y - 1]) cirs++;if (y < n - 1 && s[x][y + 1] != '*' && !v[x][y + 1]) cirs++;cal(x - 1, y); cal(x + 1, y); cal(x - 1, y - 1); cal(x - 1, y + 1);cal(x, y - 1); cal(x, y + 1); cal(x + 1, y - 1); cal(x + 1, y - 1);}}int main(){while (cin >> m >> n, m + n){double res = 0;memset(v, 0, sizeof(v));for (int i = 0; i < m; i++)cin >> s[i];for (int i = 0; i < m; i++)for (int j = 0; j < n; j++){cirs = 0; area = 0;if (s[i][j] == 'X'){search(i, j), cal(i, j);res += 1.0 * cirs / area;}}if (res > 1.0 * m * n / 4)printf("NO\n");elseprintf("YES\n");}}

2201.   Prairie dogs II

#include <iostream>#include <cstdio>#include <cstring>#define MAXN 10using namespace std;char s[MAXN][MAXN][MAXN];bool v[MAXN][MAXN][MAXN];int m, n, l, room, area, vol;void search(int z, int x, int y){if (z >= 0 && z < l && x >= 0 && y >= 0 && x < m && y < n && s[z][x][y] == 'X'){s[z][x][y] = '*'; vol++;search(z, x, y + 1); search(z, x + 1, y);search(z, x - 1, y); search(z, x, y - 1); search(z + 1, x, y); search(z - 1, x, y);}}void cal(int z, int x, int y){if (z >= 0 && z < l && x >= 0 && x < m && y >= 0 && y < n && s[z][x][y] == '*'){if (x == 0 || x == m - 1) area++;if (y == 0 || y == n - 1) area++;if (z == 0 || z == l - 1) area++;s[z][x][y] = '#'; v[z][x][y] = 1;if (x > 0 && s[z][x - 1][y] != '*' && !v[z][x - 1][y]) area++;if (x < m - 1 && s[z][x + 1][y] != '*' && !v[z][x + 1][y]) area++;if (y > 0 && s[z][x][y - 1] != '*' && !v[z][x][y - 1]) area++;if (y < n - 1 && s[z][x][y + 1] != '*' && !v[z][x][y + 1]) area++;cal(z, x - 1, y); cal(z, x + 1, y); cal(z + 1, x, y);cal(z, x, y - 1); cal(z, x, y + 1); cal(z - 1, x, y);}}int main(){while (cin >> m >> n >> l, m + n + l){double res = 0;memset(v, 0, sizeof(v));for (int k = 0; k < l; k++)for (int i = 0; i < m; i++)for (int j = 0; j < n; j++)cin >> s[k][i][j];for (int k = 0; k < l; k++)for (int i = 0; i < m; i++)for (int j = 0; j < n; j++){area = 0; vol = 0;if (s[k][i][j] == 'X'){search(k, i, j), cal(k, i, j);res += 1.0 * area / vol;}}if (res > 1.0 * m * n * l / 4)printf("NO\n");elseprintf("YES\n");}}

2805.   Prairie dogs III

#include <iostream>#include <stdio.h>#include <cmath>using namespace std;void print(int n){int w = (int)pow(3.0,n - 1);if (n == 0){cout << "@";return;}else{print(n - 1);for (int i = 0; i < w; i ++)cout << " ";print(n - 1);}} int main(){int n;while (scanf("%d",&n)!=EOF){print(n);cout << endl;}return 0;}


2818.   Prairie dogs IV

#include <iostream>#include <cstdio>#include <cstring>#define MAXN 2010using namespace std;int  a[MAXN][MAXN], flag = 1;void make(){memset(a, 0, sizeof(a));int cnt = 1, i, j;i = j = 1000; flag = 1;a[i][j++] = cnt++;a[i][j] = cnt++;while (true){if (i < 0 || i > 2000 || j < 0 || j > 2000) break;if (flag == 1){if (a[i + 1][j])a[i][++j] = cnt++;else if (!a[i + 1][j])a[++i][j] = cnt++, flag = 2;else if (i == 0 && j == 2000)break;}else if (flag == 2){if (a[i][j - 1])a[++i][j] = cnt++;else if (!a[i][j - 1])a[i][--j] = cnt++, flag = 3;}else if (flag == 3){if (a[i - 1][j])a[i][--j] = cnt++;else if (!a[i - 1][j])a[--i][j] = cnt++, flag = 4;}else if (flag == 4){if (a[i][j + 1])a[--i][j] = cnt++;else if (!a[i][j + 1])a[i][++j] = cnt++, flag = 1;}}}int main(){int x, y;make();while (cin >> x >> y){x += 1000, y += 1000;cout << a[y][x] << endl;}}

3298.   Prairie dogs V

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <queue>#define INF (-1u) >> 1#define MAXN 1010using namespace std;char map[MAXN][MAXN];int v[MAXN][MAXN];int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};int x1, y1, x2, y2, m, n, ans;struct node{int x, y, tt;bool operator < (const node & a) const{return tt > a.tt;}} test, now;void BFS(){x1--, x2--; y1--; y2--;priority_queue <node> Q;//int tx, ty, tt, vx, vy, vv;int vx, vy, vv;ans = INF;memset(v, 0, sizeof(v));test.x = x1; test.y = y1; test.tt = 0;Q.push(test);v[x1][y1] = 1;while(!Q.empty()){//tx = Q.front(); Q.pop();//ty = Q.front(); Q.pop();//tt = Q.front(); Q.pop();now = Q.top(); Q.pop();if (now.x == x2 && now.y == y2)  ans = min(ans, now.tt);for(int i = 0; i < 4; i++){vx = now.x + dir[i][0];vy = now.y + dir[i][1];if(vx < 0 || vx >= m || vy < 0 || vy >= n || v[vx][vy]) continue;v[vx][vy] = 1;if (map[vx][vy] == '.')vv = now.tt + 1;else vv = now.tt;test.x = vx; test.y = vy; test.tt = vv;Q.push(test);}}}int main(){while (cin >> m >> n, m + n){memset(map, 0, sizeof(map));for (int i = 0; i < m; i++)for (int j = 0; j < n; j++)cin >> map[i][j];cin >> x1 >> y1;cin >> x2 >> y2;BFS();cout << ans << endl;}}


0 0
原创粉丝点击