题意:给出k个长宽分别为m,n的矩阵,矩阵有若干的区域有草堆,判断草堆连通块数量和点燃所有草堆的时间
链接:http://acm.fzu.edu.cn/problem.php?pid=2150
思路:搜索连通块数量是否大于2,并且分别判断连通块数量为1和2时的情况,当连通块数量为2时,找两个草堆中的两个点花费时间最短;当连通块数量为1时,找出这个草堆中花费时间最短的两个点。
注意点:记录搜索的点的个数,重复搜索会导致TLE
以下为AC代码:
RunID | Submit Time | State | PID | Language | Time | Mem | Len | User |
---|---|---|---|---|---|---|---|---|
596980 | 2015-02-28 16:33:34 | Accepted | 2150 | GNU C++ | 640 ms | 284KB | 3851B | luminous11 |
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <list>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define mp make_pair
#define read(f) freopen(f, "r", stdin)
#define write(f) freopen(f, "w", stdout)
using namespace std;
//const double pi = acos(-1);
const int dir[4][2] = { 1,0, -1,0, 0,1, 0,-1 };
char str[15][15];
char s[15][15];
int vis[15][15];
int path[15][15];
bool num[15][15][15][15];
int m, n;
struct node{
int x, y, cnt;
node(){}
node( int _x, int _y, int _cnt ):x(_x), y(_y), cnt(_cnt){}
};
void init()
{
scanf ( "%d%d", &m, &n );
clr ( str, '.' );
clr ( num, 0 );
for ( int i = 1; i <= m; i ++ ){
scanf ( "%s", &str[i][1] );
str[i][n+1] = '.';
}
}
int dfs ( int x, int y, int k )
{
s[x][y] = '.';
vis[x][y] = k;
for ( int i = 0; i < 4; i ++ ){
int xi = x + dir[i][0];
int yi = y + dir[i][1];
if ( s[xi][yi] == '#' )
dfs ( xi, yi, k );
}
return 1;
}
int bfs ( int x, int y, int x1, int y1 )
{
int ans = 0;
queue<node> q;
clr ( path, 0x3f3f3f3f );
q.push ( node ( x, y, 0 ) );
q.push ( node ( x1, y1, 0 ) );
path[x][y] = 0;
path[x1][y1] = 0;
while ( ! q.empty() ){
node tmp = q.front();
q.pop();
for ( int i = 0; i < 4; i ++ ){
int xi = tmp.x + dir[i][0];
int yi = tmp.y + dir[i][1];
int cnt = tmp.cnt + 1;
if ( path[xi][yi] > cnt && str[xi][yi] == '#' ){
path[xi][yi] = cnt;
q.push ( node ( xi, yi, cnt ) );
ans = max ( cnt, ans );
}
}
}
return ans;
}
int solve()
{
memcpy ( s, str, sizeof ( s ) );
clr ( vis, 0 );
int block = 0;
for ( int i = 1; i <= m; i ++ ){
for ( int j = 1; j <= n; j ++ ){
if ( s[i][j] == '#' )
dfs ( i, j, ++ block );
}
}
if ( block > 2 ){
return -1;
}
if ( block == 0 ){
return 0;
}
else if ( block == 1 ){
int ans = 0x3f3f3f3f;
for ( int i = 1; i <= m; i ++ ){
for ( int j = 1; j <= n; j ++ ){
if ( str[i][j] == '#' ){
for ( int p = 1; p <= m; p ++ ){
for ( int q = 1; q <= n; q ++ ){
if ( str[p][q] == '#' ){
if ( num[i][j][p][q] == 0 ){
ans = min ( ans, bfs ( i, j, p, q ) );
num[i][j][p][q] = num[p][q][i][j] = 1;
}
}
}
}
}
}
}
return ans;
}
else if ( block == 2 ){
int ans[3];
clr ( ans, 0x3f3f3f3f );
for ( int i = 1; i <= m; i ++ ){
for ( int j = 1; j <= n; j ++ ){
if ( vis[i][j] )
ans[vis[i][j]] = min ( ans[vis[i][j]], bfs ( i, j, i, j ) );
}
}
return max ( ans[1], ans[2] );
}
}
int main()
{
ios::sync_with_stdio( false );
int t;
scanf ( "%d", &t );
for ( int i = 1; i <= t; i ++ ){
init();
printf ( "Case %d: %d\n", i, solve() );
}
return 0;
}