当前位置: 代码迷 >> 综合 >> USACO Section 2.1 The Castle (POJ 1164) floodfill
  详细解决方案

USACO Section 2.1 The Castle (POJ 1164) floodfill

热度:38   发布时间:2024-01-13 18:21:32.0

又是一道简单而又经典的搜索题,用floodfill一个房间一个房间的填充,每次都用标号标记一下,中间的围墙用二进制存储,这样搜索的时候就方便许多了。

最后再扫描一遍就能得出答案,注意扫描要从左下角开始,先往上扫描,再往右扫描。

/*
ID: sdj22251
PROG: castle
LANG: C++
*/
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#define MAX 2000000000
#define M 1007
#define LOCAL
using namespace std;
int mp[55][55];
int room[2555];
int roomnum[55][55];
int sum, size, m, n;
bool v[55][55];
void dfs(int i, int j)
{v[i][j] = true;roomnum[i][j] = sum;size++;if((mp[i][j] & 1) == 0 && j >= 2 && !v[i][j - 1]) dfs(i, j - 1);if((mp[i][j] & 2) == 0 && i > 1 && !v[i - 1][j]) dfs(i - 1, j);if((mp[i][j] & 4) == 0 && j < m && !v[i][j + 1]) dfs(i, j + 1);if((mp[i][j] & 8) == 0 && i < n && !v[i + 1][j]) dfs(i + 1, j);
}
int main()
{
#ifdef LOCALfreopen("castle.in","r",stdin);freopen("castle.out","w",stdout);
#endifint i, j;scanf("%d%d", &m, &n);for(i = 1; i <= n; i++){for(j = 1; j <= m; j++){scanf("%d", &mp[i][j]);}}sum = 0;for(i = 1; i <= n; i++){for(j = 1; j <= m; j++){if(!v[i][j]){size = 0;dfs(i, j);room[sum++] = size;}}}int mx = -1;for(i = 0; i < sum; i++){if(mx < room[i])mx = room[i];}printf("%d\n%d\n", sum, mx);mx = -1;char c;int r, l;for(j = 1; j <= m; j++){for(i = n; i >= 1; i--){if((mp[i][j] & 2) != 0 && i > 1){if(roomnum[i][j] != roomnum[i - 1][j]){int tmp = room[roomnum[i][j]] + room[roomnum[i - 1][j]];if(tmp > mx){r = i;l = j;c = 'N';mx = tmp;}}}if((mp[i][j] & 4) != 0 && j < m){if(roomnum[i][j] != roomnum[i][j + 1]){int tmp = room[roomnum[i][j]] + room[roomnum[i][j + 1]];if(tmp > mx){r = i;l = j;c = 'E';mx = tmp;}}}}}printf("%d\n%d %d %c\n", mx, r, l, c);return 0;
}


  相关解决方案