1726번 - 로봇
진짜 어디가 틀렸는지 모르겠어요...
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
struct Data{
int x,y,dir,cnt;
};
int xx, yy; int map[101][101]; bool chk[101][101][5];
int dir[5][5]={{0,0,0,0,0},{0,0,0,1,1},{0,0,0,1,1},{0,1,1,0,0},{0,1,1,0,0}};
Data Start,End;
int dx[5] = {0,1,-1,0,0};
int dy[5] = {0,0,0,1,-1};
void init();
void solve();
bool Range(int x, int y){
if(x<1 || y<1 || x>xx || y>yy || map[y][x]==1)
return false;
else
return true;
}
int main(){
init();
solve();
return 0;
void init(){
cin >> yy >> xx;
for(int i=1; i<=yy; i++){
for(int j=1; j<=xx; j++)
scanf("%d", &map[i][j]);
cin>> Start.y >> Start.x >> Start.dir;
cin>> End.y >> End.x >> End.dir;
void solve(){
queue<Data>q;
q.push(Start);
chk[Start.y][Start.x][Start.dir]=1;
while(!q.empty()){
Data now;
now=q.front();
q.pop();
if(now.x == End.x && now.y == End.y && now.dir == End.dir){
cout << now.cnt<<endl;
break;
for(int i=1; i<5; i++){
Data a =now;
if(chk[now.y][now.x][i] ==0 && dir[now.dir][i] !=0){
a.dir=i;
a.cnt +=dir[now.dir][i];
chk[now.y][now.x][i]=1;
q.push(a);
for(int i=1; i<4; i++){
Data next = now;
next.cnt = now.cnt+1;
next.y = now.y + i*dy[now.dir];
next.x = now.x + i*dx[now.dir];
if(Range(next.x, next.y) && chk[next.y][next.x][next.dir]==0){
chk[next.y][next.x][next.dir]=1;
q.push(next);
}else
댓글을 작성하려면 로그인해야 합니다.
didwlvv 6년 전
진짜 어디가 틀렸는지 모르겠어요...
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
struct Data{
int x,y,dir,cnt;
};
int xx, yy; int map[101][101]; bool chk[101][101][5];
int dir[5][5]={{0,0,0,0,0},{0,0,0,1,1},{0,0,0,1,1},{0,1,1,0,0},{0,1,1,0,0}};
Data Start,End;
int dx[5] = {0,1,-1,0,0};
int dy[5] = {0,0,0,1,-1};
void init();
void solve();
bool Range(int x, int y){
if(x<1 || y<1 || x>xx || y>yy || map[y][x]==1)
return false;
else
return true;
}
int main(){
init();
solve();
return 0;
}
void init(){
cin >> yy >> xx;
for(int i=1; i<=yy; i++){
for(int j=1; j<=xx; j++)
scanf("%d", &map[i][j]);
}
cin>> Start.y >> Start.x >> Start.dir;
cin>> End.y >> End.x >> End.dir;
}
void solve(){
queue<Data>q;
q.push(Start);
chk[Start.y][Start.x][Start.dir]=1;
while(!q.empty()){
Data now;
now=q.front();
q.pop();
if(now.x == End.x && now.y == End.y && now.dir == End.dir){
cout << now.cnt<<endl;
break;
}
for(int i=1; i<5; i++){
Data a =now;
if(chk[now.y][now.x][i] ==0 && dir[now.dir][i] !=0){
a.dir=i;
a.cnt +=dir[now.dir][i];
chk[now.y][now.x][i]=1;
q.push(a);
}
}
for(int i=1; i<4; i++){
Data next = now;
next.cnt = now.cnt+1;
next.y = now.y + i*dy[now.dir];
next.x = now.x + i*dx[now.dir];
if(Range(next.x, next.y) && chk[next.y][next.x][next.dir]==0){
chk[next.y][next.x][next.dir]=1;
q.push(next);
}else
break;
}
}
}