UVa 871 - Counting Cells in a Blob
/*
Lionel Messi is such a player that you may catch him, you may touch him, you may feel him
and definitely you may Love him.
Lionel Messi is Messi. A little Magician in this World.
*/
// Accepted
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")
#define psb push_back
#define mset(c,v) memset(c , v , sizeof c)
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back
typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;
const int mod = 1000007;
const int high = 30;
char adj[high][high];
int row=0, col=0, visited[high][high], cnt=0;
void DFS(int x , int y)
{
if(x < 0 || x >= row || y < 0 || y >= col) return;
if(adj[x][y] != '1') return;
if(adj[x][y] == '1') adj[x][y] = '0';
cnt+=1;
visited[x][y] = 1;
DFS(x , y+1); // horizontal
DFS(x , y-1); // horizontal
DFS(x+1 , y); // vertical
DFS(x-1 , y); // vertical
DFS(x+1 , y+1); // diagonal
DFS(x-1 , y+1); // diagonal
DFS(x+1 , y-1); // diagonal
DFS(x-1 , y-1); // diagonal
}
int main()
{
fast;
int i, j ,test , nline=0;
//cin >> test;
sf("%d", &test);
getchar();
getchar();
while(test--)
{
row = -1;
mset(visited , 0);
mset(adj, '\0');
if(nline) pf("\n");
nline = 1;
while(gets(adj[++row]))
{
int l = strlen(adj[row]);
if(!l) break;
}
int mx = 0;
for(i=0; i<row; i++)
{
col = strlen(adj[i]);
for(j=0; j<col; j++)
{
cnt = 0;
if(adj[i][j] == '1' && visited[i][j] == 0)
{
DFS(i , j);
//cout << cnt << "\n";
mx = max(mx , cnt);
cnt = 0;
}
}
}
pf("%d\n", mx);
// for(i=0; i<row; i++)
// {
// col = strlen(adj[i]);
//
// for(j=0; j<col; j++)
// {
// cout << adj[i][j] << "; ";
// }
//
// cout << "\n";
// }
}
return 0;
}
Lionel Messi is such a player that you may catch him, you may touch him, you may feel him
and definitely you may Love him.
Lionel Messi is Messi. A little Magician in this World.
*/
// Accepted
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")
#define psb push_back
#define mset(c,v) memset(c , v , sizeof c)
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back
typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;
const int mod = 1000007;
const int high = 30;
char adj[high][high];
int row=0, col=0, visited[high][high], cnt=0;
void DFS(int x , int y)
{
if(x < 0 || x >= row || y < 0 || y >= col) return;
if(adj[x][y] != '1') return;
if(adj[x][y] == '1') adj[x][y] = '0';
cnt+=1;
visited[x][y] = 1;
DFS(x , y+1); // horizontal
DFS(x , y-1); // horizontal
DFS(x+1 , y); // vertical
DFS(x-1 , y); // vertical
DFS(x+1 , y+1); // diagonal
DFS(x-1 , y+1); // diagonal
DFS(x+1 , y-1); // diagonal
DFS(x-1 , y-1); // diagonal
}
int main()
{
fast;
int i, j ,test , nline=0;
//cin >> test;
sf("%d", &test);
getchar();
getchar();
while(test--)
{
row = -1;
mset(visited , 0);
mset(adj, '\0');
if(nline) pf("\n");
nline = 1;
while(gets(adj[++row]))
{
int l = strlen(adj[row]);
if(!l) break;
}
int mx = 0;
for(i=0; i<row; i++)
{
col = strlen(adj[i]);
for(j=0; j<col; j++)
{
cnt = 0;
if(adj[i][j] == '1' && visited[i][j] == 0)
{
DFS(i , j);
//cout << cnt << "\n";
mx = max(mx , cnt);
cnt = 0;
}
}
}
pf("%d\n", mx);
// for(i=0; i<row; i++)
// {
// col = strlen(adj[i]);
//
// for(j=0; j<col; j++)
// {
// cout << adj[i][j] << "; ";
// }
//
// cout << "\n";
// }
}
return 0;
}
Comments
Post a Comment