仅以此题纪念打铁的南阳。这题与南阳下棋题相似度实在太高……需要注意的是此题一面墙一幅画,而南阳是一个节点一个棋子。转化成模型即是此题一个节点存在四面,与下棋题有一定区别。DFS练手题,别问我南阳为什么会卡这种题。
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <stack>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std;
#define N 1005
#define INF 0x3f3f3f3f;
int a[N][N];
int n,m,k;
int idm[N*N];
struct A{
int idx,sum;
}num[N][N];
void dfs(int r,int c,int id,int su){
if (r<1||r>m||c<1||c>n) return;
if (a[r][c]!=1||num[r][c].idx>0) return;
num[r][c].idx=id;
if(a[r+1][c]==0) su++;
if(a[r-1][c]==0) su++;
if(a[r][c+1]==0) su++;
if(a[r][c-1]==0) su++;
num[r][c].sum=su;
for (int dr=-1; dr<=1; dr++) {
for (int dc=-1;dc<=1; dc++) {
if ((dr!=0||dc!=0)&&dr*dc==0) {
idm[id]=max(idm[id],su);
dfs(r+dr, c+dc, id,idm[id]);
}
}
}
}
int main(){
string temp;
while (cin>>n>>m>>k) {
memset(a, 0, sizeof(a));
memset(num, 0, sizeof(num));
memset(idm, 0, sizeof(idm));
for (int j=1; j<=n; j++) {
cin>>temp;
for (int i=1; i<=m; i++) {
if (temp[i-1]=='*') a[i][j]=0;
else a[i][j]=1;
}
}
int idflag=0;
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
if (num[j][i].idx==0&&a[j][i]==1)
dfs(j, i, ++idflag, 0);
}
}
int x,y;
while (k--) {
cin>>y>>x;
printf("%d\n",idm[num[x][y].idx]);
}
}
}