UVA 722 Solution- Lakes

UVA 722 Solution- Lakes


uva id: shoaib05
Accepted Time: 0.000

#include<cstdio>
#include<cstring>
#include<stack>

using namespace std;

typedef pair<int,int> pr;


int main() 
{
int N, a, b, n, m,ans,i,x,y;
char inp[110];
char graph[100][100];
bool vis[100][100];
int dx[4] = { 0, 0, -1, 1 };
int dy[4] = { 1, -1, 0, 0 };
stack<pr> st;
pr p;
    scanf("%d", &N);
    while(N--) {
        scanf("%d %d", &a, &b);
        a--; b--;
        memset(vis, false, sizeof vis);
        n = 0;
getchar();
        while(gets(inp) && strlen(inp) > 0) {
            strcpy(graph[n], inp);
            n++;
        }
        m = strlen(graph[0]);
ans=0;
if(graph[a][b]=='0')
{
ans++;
p=pair<int,int>(a,b);
vis[a][b]=true;
st.push(p);
}
while(!st.empty())
{
p=st.top();
st.pop();
a=p.first;
b=p.second;
for(i=0;i<4;i++)
{
x=a+dx[i];
y=b+dy[i];
if(x>=0 && x<n && y>=0 && y<m && graph[x][y]=='0' && vis[x][y]==false)
{
vis[x][y]=true;
p=pair<int,int>(x,y);
st.push(p);
ans++;
}
}
}
printf("%d\n", ans);
        if(N) printf("\n");
    }  
return 0;
}

Comments

Popular posts from this blog

uva 679 - Dropping Balls Solution

uva 481 - What Goes Up Solution

uva-10077 Solution --- The Stern-Brocot Number System