Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<vector>
- #include<stdio.h>
- #include<iostream>
- #include<queue>
- using namespace std;
- char grid[1010][1010];
- int vis[1010][1010];
- int n,m;
- int dx[4]={0,0,1,-1};
- int dy[4]={-1,1,0,0};
- void bfs(int srcx,int srcy)
- {
- int i,vx,vy,ux,uy;
- vis[srcx][srcy]=1;
- queue< pair<int,int> > Q;
- Q.push( make_pair(srcx,srcy) );
- while(!Q.empty())
- {
- ux=Q.front().first;
- uy=Q.front().second;
- Q.pop();
- for(int k=0;k<4;k++)
- {
- vx=ux+dx[k];
- vy=uy+dy[k];
- if(vx>=0 && vx<n && vy>=0 && vy<m && vis[vx][vy]==0)
- {
- vis[vx][vy]=vis[ux][uy]+1;
- Q.push(make_pair(vx,vy));
- }
- }
- }
- }
- int main()
- {
- freopen("in.txt","r",stdin);
- int i,j,k,i2,j2;
- cin>>n;
- n=2*n-1;
- m=n;
- bfs(n/2,n/2);
- for(i=0;i<n;i++)
- {
- for(j=0;j<m;j++)
- {
- cout<<vis[i][j];
- }
- cout<<endl;
- }
- return 0;
- }
- ///dfs http://pastebin.com/T0GyaHQX
- ///shortest path using dfs http://pastebin.com/HEtHBfrR
- ///bfs http://pastebin.com/bAaeTuCm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement