Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define pb push_back
- #define ll long long
- #define pii pair<int,int>
- #define pll pair<ll,ll>
- #define M 100007
- #define INF 1e9
- #define INFL 1e18
- #define PI acos(-1)
- #define mp make_pair
- #define fast_in_out ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
- int fx[]= {+1,-1,+0,+0};
- int fy[]= {+0,+0,+1,-1};
- int cell[110][110];
- int x,y;
- int visited[110][110];
- int dis[110][110];
- int bfs(int row,int col)
- {
- memset(visited,0,sizeof visited);
- memset(dis,0,sizeof dis);
- visited[x][y]=1;
- dis[x][y]=0;
- queue<pii>q;
- int ans=0;
- // cout<<row<<" "<<col<<endl;
- q.push(mp(x,y));
- while(!q.empty())
- {
- pii u=q.front();
- q.pop();
- for(int i=0;i<4;i++)
- {
- int tx=u.first+fx[i];
- int ty=u.second+fy[i];
- if(tx>=0 && tx<row && ty>=0 && ty<col && cell[tx][ty]!=1 && visited[tx][ty]==0)
- {
- visited[tx][ty]=1;
- dis[tx][ty]=dis[u.first][u.second]+1;
- ans++;
- q.push(mp(tx,ty));
- }
- }
- }
- return ans+1;
- }
- int main()
- {
- fast_in_out;
- // freopen("722output.txt","w",stdout);
- int q;
- scanf("%d ",&q);
- while(q--)
- {
- //freopen("input.txt","r",stdin);
- char s[100];
- gets(s);
- sscanf(s,"%d%d",&x,&y);
- string a[100];
- int cnt=0;
- char str[100];
- getchar();
- while(gets(str) && strlen(str)>0)
- {
- a[cnt++]=str;
- }
- int row=cnt;
- int col=a[0].length();
- // cout<<row<<" "<<col<<endl;
- memset(cell,0,sizeof cell);
- for(int i=0;i<cnt;i++)
- {
- for(int j=0;j<col;j++)
- {
- if(a[i][j]=='1')
- {
- cell[i][j]=1;
- }
- }
- }
- x--;
- y--;
- int ans=bfs(row,col);
- cout<<ans<<endl;
- if(q)
- cout<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment