Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <vector>
- #include <algorithm>
- #include <cstring>
- #include <queue>
- using namespace std;
- char loch[1002][1002];
- int dist[1002][1002][2];
- #define NORM 0
- #define ASTR 1
- struct pole
- {
- int x, y;
- bool lev;
- };
- bool free(int x, int y)
- {
- return loch[x][y]=='.' || loch[x][y]=='%';
- }
- bool free(pole asd)
- {
- return free(asd.x, asd.y);
- }
- bool passable(int x, int y, bool stan)
- {
- return free(x, y) ||
- (loch[x][y]=='+' && stan==NORM) ||
- (loch[x][y]=='o' && stan==ASTR);
- }
- bool passable(pole asd, bool stan)
- {
- return passable(asd.x, asd.y, stan);
- }
- void process(int x, int y, bool lev, pole now, queue<pole> &tocheck)
- {
- if(passable(x, y, lev) && dist[x][y][lev]==-1)
- {
- dist[x][y][lev]=dist[now.x][now.y][now.lev]+1;
- pole kurde;
- kurde.x=x; kurde.y=y; kurde.lev=lev;
- tocheck.push(kurde);
- }
- }
- int main()
- {
- int ile;
- scanf("%d", &ile);
- while(ile--)
- {
- int w,h;
- scanf("%d %d", &w, &h);
- for(int x=0; x<=w+1; x++) for(int y=0; y<=h+1; y++) dist[x][y][NORM]=dist[x][y][ASTR]=-1;
- pole start, meta;
- // otaczamy scianami
- for(int i=0; i<=w+1; i++) loch[i][0 ] = loch[i][h+1] = '#';
- for(int i=1; i<=h ; i++) loch[0 ][i] = loch[w+1][i] = '#';
- // czytamy
- char derp;
- for(int x=1; x<=w; x++) for(int y=1; y<=h; y++)
- {
- do{scanf("%c", &derp);} while(derp=='\n' || derp=='\r');
- if(derp=='>')
- {
- meta.x=x; meta.y=y; meta.lev=NORM;
- derp='.';
- }
- else if(derp=='@')
- {
- start.x=x; start.y=y; start.lev=NORM;
- derp='.';
- }
- loch[x][y] = derp;
- }
- // magia
- pole now;
- queue<pole> tocheck;
- tocheck.push(start);
- dist[start.x][start.y][start.lev]=0;
- while(!tocheck.empty())
- {
- now=tocheck.front();
- tocheck.pop();
- process(now.x-1, now.y, now.lev, now, tocheck);
- process(now.x+1, now.y, now.lev, now, tocheck);
- process(now.x, now.y-1, now.lev, now, tocheck);
- process(now.x, now.y+1, now.lev, now, tocheck);
- if(loch[now.x][now.y]=='%')
- {
- process(now.x-1, now.y, !now.lev, now, tocheck);
- process(now.x+1, now.y, !now.lev, now, tocheck);
- process(now.x, now.y-1, !now.lev, now, tocheck);
- process(now.x, now.y+1, !now.lev, now, tocheck);
- }
- }
- if(dist[meta.x][meta.y][meta.lev]>=0) printf("%d\n", dist[meta.x][meta.y][meta.lev]);
- else printf("NIE\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement