poj 3414 bfs 물 문제 인 데 귀찮아 요.

7415 단어 poj
랄 라 라, 이거 너무 성취 감 넘 쳐!!!!115 줄 코드 입 니 다.
용기 a, 용기 b, 그리고 용량 c 를 드 립 니 다.-- 제 한 된 절 차 를 통 해 c 의 목표 상 태 를 달성 할 수 있 을 까?
음, path 배열 에 관 해 서 는 사실 또 대신 을 참조 하 는 것 은 너무 느끼 해서 조작 6 가 지 를 해 칠 수 밖 에 없다. 1. a FILL (1) 을 가득 채 우 고 2. b FILL (2) 을 가득 채 우 고 3. a DROP (1) 를 비우 고 4. b DROP (2) 를 비우 고 5. A 를 B POUR (1, 2) 에 붓는다.
알고리즘: bfs, 플러스 그룹? ps and 인내심, 즉 문 제 를 다 쓰 는 인내심 과 잘못 고 치 는 인내심 [울 음]
다음은 codes 입 니 다.
poj 3414
228k 0ms
#include<cstdio>
#include<queue>
#include<cstring>

using namespace std;

int m,n,k;
int vis[105][105];
char opr[20][20]= {"FILL(1)" , "FILL(2)" , "DROP(1)" , "DROP(2)" , "POUR(1,2)" , "POUR(2,1)" };     // 6   

struct node
{
    int x,y,step;//         
    int path[200]; //          //!!
};
void bfs( )
{
    int i,j;
    int kx,ky;
    memset(vis,0,sizeof(vis));
    queue<node>q;
    node now,next;//          
    now.x=0;
    now.y=0;
    now.step=0;
    q.push(now);
    vis[0][0]=1;
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(now.x==k||now.y==k)//       
        {
            printf("%d
"
,now.step); for( i=1; i<=now.step; i++) { printf("%s
"
,opr[now.path[i]]); } return; } for(i=1; i<=6; i++) // { if(i == 1 ) // , 1; { next.y=now.y; next.x=m; now.path[now.step+1]=0; } else if(i == 2)//sec,fill 2 { next.x=now.x; next.y=n; now.path[now.step+1]=1; } else if(i == 3)//third,drop 1; { next.y=now.y; next.x=0; now.path[now.step+1]=2; } else if(i == 4)// fourth,drop 2 { next.x=now.x; next.y=0; now.path[now.step+1]=3; } else if(i == 5) { if(n-now.y>now.x) // pour { next.x=0; next.y=now.x+now.y; } else { next.y=n; next.x=now.x-n+now.y; } now.path[now.step+1]=4; } else if(i == 6) { if(m-now.x>now.y)// { next.y=0; next.x=now.x+now.y; } else// { next.x=m;// m next.y=now.y-m+now.x; } now.path[now.step+1]=5; } if(vis[next.x][next.y] == 1) continue; vis[next.x][next.y]=1; next.step=now.step+1; for(j=1; j<=next.step; j++) //!!!!!! operation, { next.path[j]=now.path[j]; } q.push(next); } } printf("impossible
"
); return; } int main() { int i; scanf("%d%d%d",&m,&n,&k); bfs(); return 0; }

좋은 웹페이지 즐겨찾기