PKU3414 Pots

题目链接:http://poj.org/problem?id=3414

这题以前碰到,没做,感觉好麻烦的样子。

今天突然想起这题,反应过来,这不就是一个BFS吗。

用一个二维数组vis[a][b]来记录a,b是否出现过,然后6个操作搜索一遍,能入队的入队。

我一开始没发现内存限制这么大,所以写的时候比较考虑内存。。

用了一个char数组来存6个操作的名字

然后在一个路径数组中存了所有操作,每一个节点都记录了这个操作在数组里的下标

然后再用一个结构体来读取路径,最后用了一个栈也是因为写起来比较方便。。。

a,b的范围都是<=100,所以vis数组要开101*101。

写的时候没有多想,一路想一路写下来,有些地方写得不太好,代码显得很长,4000多B,大概写了40分钟,还是需要进步啊!

不过能1次AC也是比较惊讶的,毕竟只测了样例就交了。。。

题目:Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11017 Accepted: 4674 Special Judge

Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

  1. FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap;
  2. DROP(i)      empty the pot i to the drain;
  3. POUR(i,j)    pour from pot i to pot j; after this operation either the potj is full (and there may be some water left in the poti), or the poti is empty (and all its contents have been moved to the potj).

Write a program to find the shortest possible sequence of these operations that will yield exactlyC liters of water in one of the pots.

Input

On the first and only line are the numbers A, B, andC. These are all integers in the range from 1 to 100 andC≤max(A,B).

Output

The first line of the output must contain the length of the sequence of operationsK. The followingK lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

Sample Input

3 5 4

Sample Output

6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
代码如下:

#include<functional>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cctype>
#include<string>
#include<stack>
#include<queue>
#include<cmath>
#include<set>
#include<map>
using namespace std;
int sa,sb,target;
bool vis[101][101];
char output[6][10]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(2,1)","POUR(1,2)"};
//                     0         1         2         3         4           5
typedef struct node{
    int index;
    int a,b;
    int step;
};
typedef struct pp{
    int now,pre;
};
int px;
pp path[10001];
int bfs(node &sourse)
{
    queue<node> Q;
    node start,use;
    start.step=0;
    start.index=0;
    start.a=0;
    start.b=0;
    vis[0][0]=1;
    path[px].pre=-1;
    path[px++].now=-1;
    Q.push(start);
    while(!Q.empty())
    {
        start=Q.front();
        Q.pop();
        if(start.a==target||start.b==target)
        {
            sourse=start;
            return start.step;
        }
        if(vis[sa][start.b]==false)
        {//fill 1
            vis[sa][start.b]=true;
            use.a=sa;
            use.b=start.b;
            use.step=start.step+1;
            use.index=px;
            path[px].pre=start.index;
            path[px++].now=0;
            Q.push(use);
        }
        if(vis[start.a][sb]==false)
        {//fill 2
            vis[start.a][sb]=true;
            use.a=start.a;
            use.b=sb;
            use.step=start.step+1;
            use.index=px;
            path[px].pre=start.index;
            path[px++].now=1;
            Q.push(use);
        }
        if(vis[0][start.b]==false)
        {//drop 1
            vis[0][start.b]=true;
            use.a=0;
            use.b=start.b;
            use.step=start.step+1;
            use.index=px;
            path[px].pre=start.index;
            path[px++].now=2;
            Q.push(use);
        }
        if(vis[start.a][0]==false)
        {//drop 2
            vis[start.a][0]=true;
            use.a=start.a;
            use.b=0;
            use.step=start.step+1;
            use.index=px;
            path[px].pre=start.index;
            path[px++].now=3;
            Q.push(use);
        }
        if(start.a<sa&&start.b>0)
        {//pour 2 1
            int tempa,tempb;
            tempa=(start.a+start.b>=sa)?sa:start.a+start.b;
            tempb=(sa-start.a>=start.b)?0:start.b-sa+start.a;
            if(vis[tempa][tempb]==false)
            {
                vis[tempa][tempb]=true;
                use.a=tempa;
                use.b=tempb;
                use.step=start.step+1;
                path[px].pre=start.index;
                use.index=px;
                path[px++].now=4;
                Q.push(use);
            }
        }
        if(start.a>0&&start.b<sb)
        {
            int tempa,tempb;
            tempa=(sb-start.b>=start.a)?0:start.a-sb+start.b;
            tempb=(start.a+start.b>=sb)?sb:start.a+start.b;
            if(vis[tempa][tempb]==false)
            {
                vis[tempa][tempb]=true;
                use.a=tempa;
                use.b=tempb;
                use.step=start.step+1;
                path[px].pre=start.index;
                use.index=px;
                path[px++].now=5;
                Q.push(use);
            }
        }
    }
    return -1;
}
int main()
{
    while(scanf("%d%d%d",&sa,&sb,&target)!=EOF)
    {
        getchar();
        memset(vis,false,sizeof(vis));
        memset(path,0,sizeof(path));
        px=0;
        node sourse;
        sourse.a=-1;
        sourse.b=-1;
        int ans=bfs(sourse);
        if(ans<0){
            printf("impossible\n");
            continue;
        }
        printf("%d",ans);
        stack<int> st;
        while(sourse.index>=0)
        {
            st.push(path[sourse.index].now);
            sourse.index=path[sourse.index].pre;
        }
        while(!st.empty())
        {
            printf("%s\n",output[st.top()]);
            st.pop();
        }
    }
    return 0;
}




一、综合实战—使用极轴追踪方式绘制信号灯 实战目标:利用对象捕捉追踪和极轴追踪功能创建信号灯图形 技术要点:结合两种追踪方式实现精确绘图,适用于工程制图中需要精确定位的场景 1. 切换至AutoCAD 操作步骤: 启动AutoCAD 2016软件 打开随书光盘中的素材文件 确认工作空间为"草图与注释"模式 2. 绘图设置 1)草图设置对话框 打开方式:通过"工具→绘图设置"菜单命令 功能定位:该对话框包含捕捉、追踪等核心绘图辅助功能设置 2)对象捕捉设置 关键配置: 启用对象捕捉(F3快捷键) 启用对象捕捉追踪(F11快捷键) 勾选端点、中心、圆心、象限点等常用捕捉模式 追踪原理:命令执行时悬停光标可显示追踪矢量,再次悬停可停止追踪 3)极轴追踪设置 参数设置: 启用极轴追踪功能 设置角度增量为45度 确认后退出对话框 3. 绘制信号灯 1)绘制圆形 执行命令:"绘图→圆→圆心、半径"命令 绘制过程: 使用对象捕捉追踪定位矩形中心作为圆心 输入半径值30并按Enter确认 通过象限点捕捉确保圆形位置准确 2)绘制直线 操作要点: 选择"绘图→直线"命令 捕捉矩形上边中点作为起点 捕捉圆的上象限点作为终点 按Enter结束当前直线命令 重复技巧: 按Enter可重复最近使用的直线命令 通过圆心捕捉和极轴追踪绘制放射状直线 最终形成完整的信号灯指示图案 3)完成绘制 验证要点: 检查所有直线是否准确连接圆心和象限点 确认极轴追踪的45度增量是否体现 保存绘图文件(快捷键Ctrl+S)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值