爱菲儿陌陌直播回放:A题 A题 A题 A题 A题 A题 A题

来源:百度文库 编辑:高校问答 时间:2024/04/28 14:59:32
#include<stdio.h>
#include<iostream>
#include<memory>
using namespace std;
#define MAX 100
int tn,ti;
int m,n;
int i,j;
bool board[MAX+1][MAX+1];
bool label[MAX+1][MAX+1];
bool visited[MAX+1][MAX+1];
struct sdir{
int x;
int y;

};
sdir dir[4] = {{-1,0},{1,0},{0,-1},{0,1}};
void init()
{
for(i = 0; i< m; i++)
for(j = 0; j < n; j++)
{
cin>>board[i][j];
label[i][j] = board[i][j];
}
}
bool judge(int x,int y)
{

if(x>=0&&x<m&&y>=0&&y<n&&label[x][y] == true)
return true;
else return false;

}
int calc(int x,int y,int count)
{
bool templ = false;
if(board[x][y] == true){
if(label[x][y] == true)
{

templ = true;
}
int temp;
int i;
for(i = 0; i< 4; i++)
{
if(judge(x+dir[i].x,y+dir[i].y))
{
if(templ == true)
{
count ++;
label[x][y] = false;
label[x+dir[i].x][y+dir[i].y] = false;
}
visited[x+dir[i].x][y+dir[i].y] = true;
temp = calc(x+dir[i].x,y+dir[i].y,count);
if(temp > count)
count = temp;
if(templ == true)
{
count --;
label[x][y] = true;
label[x+dir[i].x][y+dir[i].y] = true;
}
}
}
}
return count;
}
int cal()
{
memset(visited,0,sizeof(visited));
int sum = 0;
int count = 0;
int i,j;
for(i = 0;i < m; i++)
for(j = 0;j < n;j++)
{

if(label[i][j] == true&&visited[i][j] == true)
{
count = 0;
sum += calc(i,j,count);

}

}
return sum;
}
int main()
{
cin>>tn;
ti = tn;
while(ti--)
{
init();
cout<<cal()<<endl;
}

}