URAL-1101. Robot in the Field

3 篇文章 0 订阅
1 篇文章 0 订阅

1、知识点:动态规划
2、思路:此题最大的难点恐怕还是表达式处理,我用的是栈,代码比较长,但注释比较详细。

/*
*/

#include <stdio.h>
#include <string.h>
#include <string>
#include <stack>
using namespace std;
#define MAXN 100+10                     //N,M,K最大值 
#define MAXEXPRELEN 250+10

struct Point{                           //点 
    int x;
    int y;
    int dir;                            //标记到当前点的方向 
};
Point fork_point[MAXN];                 //分叉点 
Point cur_poi;                          //当前点 

struct Invert{                          //定义转换点
    int a;
    int b;
    char c[5];
};
Invert inv[MAXN];                       //转换点                        
char sent[MAXEXPRELEN];                 //表达式 
int reg_arr[26+5];                      //寄存器数组 
int N,                                  //field
    M,                                  //fork 
    K;                                  //register-invert-point

int get_val(const char *formu);         //计算表达式的值 
int get_pri(string str);                //获取优先级 
bool is_fork();                         //判断当前点是否分叉 
void next_posi(int res, Point &poi);    //根据当前点计算下一个位置
bool is_out(Point poi);                 //检查新点是否出界 
bool is_invert(int &reg);               //检查当前点是否转换点,若是可得寄存器编号 
int main()
{
    memset(reg_arr, 0, sizeof(reg_arr));
    gets(sent);
    scanf("%d%d%d", &N, &M, &K);
    for(int i=0; i<M; i++)
        scanf("%d%d", &(fork_point[i].x), &(fork_point[i].y));

    for(int j=0; j<K; j++)
        scanf("%d%d%s", &(inv[j].a), &(inv[j].b), &(inv[j].c));

    cur_poi.x = 1;
    cur_poi.y = 0;
    cur_poi.dir = 1;                        //(0,0)->(1,0):右
    printf("%d %d\n", 0, 0);
    printf("%d %d\n", 1, 0);
    Point new_poi;
    int inv_reg;                            //转换的寄存器编号 
    for(;;){
        int res = -1;                       //默认直走 
        if(is_fork())                       //如果站在分叉点 
            res = get_val(sent);            //左,右? 

        if(is_invert(inv_reg))              //如果站在转换点 
            reg_arr[inv_reg] = 1 - reg_arr[inv_reg];

        next_posi(res, new_poi);            //计算下一个点 
        bool ch = is_out(new_poi);          //检查新点是否出界 
        if(!ch){
            printf("%d %d\n", new_poi.x, new_poi.y);
            cur_poi.x = new_poi.x;
            cur_poi.y = new_poi.y;
            cur_poi.dir = new_poi.dir;
        }
        else
            break;
    }

    return 0;
}

int get_val(const char *formu)
{
    string expre = formu; 
    expre += "#";
    int len = expre.length();
    stack<string> sym;
    stack<string> oper;
    string str="";              //临时字符串 
    string top_str="";          //栈顶运算符
    string top_oper="";         //栈顶操作数 
    int str_len=0;
    sym.push("#");
    for(int i=0; i<len; i++){
        str += expre[i];
        str_len++;
        if(str == " "){                             //如果是空格 
            str = ""; 
            str_len = 0;
            continue;
        }
        if(isalpha(str[0])){
            int j;
            for(j=i+1; j<len; j++)
                if(isalpha(expre[j])){
                    str += expre[j];
                    str_len++;
                }
                else
                    break;
            i = j-1;                                //其它字符,i移至最后一个有效字符 
        }

        if( str=="TRUE" || str=="FALSE" || 
            (isalpha(str[0]) && str_len == 1))      //寄存器或真值 
            oper.push(str);
        else{                                       //操作符 或 括号 
            if(get_pri(str) > get_pri(sym.top()) ||
                (sym.top()=="("&&str!=")") || str=="NOT")//>栈顶优先级 或 栈顶是左括号 或 "NOT"(NOT是右结合) 
                sym.push(str);
            else if(sym.top()=="(" && str==")")
                sym.pop();
            else{                                   //<栈顶优先级 或 右括号 或 "#" 
                while(sym.top()!="#" && get_pri(str)<=get_pri(sym.top()) && sym.top()!="("){
                    bool res = 0;                   //临时结果
                    string res_str = "";            //临时结果的字符串形式(TRUE,FALSE) 
                    top_str = sym.top();            //弹出栈顶运算符 
                    sym.pop();
                    top_oper = oper.top();          //弹出第一个操作数 
                    oper.pop();
                    if(top_str == "NOT"){           //单操作数 (只有NOT一个) 
                        if(top_oper == "TRUE")
                            res = 0;
                        else if(top_oper == "FALSE")
                            res = 1;
                        else
                            res = 1 - reg_arr[top_oper[0] - 'A'];
                    }
                    else{                           //双操作数 
                        string sec_oper = oper.top();       //第二个操作数 
                        oper.pop();
                        if(top_str == "AND"){               //AND
                            if(top_oper == "TRUE"){
                                if(sec_oper == "TRUE")
                                    res = 1;
                                else if(sec_oper == "FALSE")
                                    res = 0;
                                else
                                    res = reg_arr[sec_oper[0] - 'A'];
                            }
                            else if(top_oper == "FALSE")
                                res = 0;
                            else{                           //第一个操作数是寄存器 
                                if(!reg_arr[top_oper[0] - 'A'])
                                    res = 0;
                                else{
                                    if(sec_oper == "TRUE")
                                        res = 1;
                                    else if(sec_oper == "FALSE")
                                        res = 0;
                                    else
                                        res = reg_arr[sec_oper[0] - 'A'];
                                }
                            } 
                        }
                        else{                               //OR
                            if(top_oper == "TRUE")
                                res = 1;
                            else if(top_oper == "FALSE"){
                                if(sec_oper == "TRUE")
                                    res = 1;
                                else if(sec_oper == "FALSE")
                                    res = 0;
                                else
                                    res = reg_arr[sec_oper[0] - 'A'];
                            }
                            else{                           //第一个操作数是寄存器 
                                if(reg_arr[top_oper[0] - 'A'])
                                    res = 1;
                                else{                       //第一个操作数为0 
                                    if(sec_oper == "TRUE")
                                        res = 1;
                                    else if(sec_oper == "FALSE")
                                        res = 0;
                                    else
                                        res = reg_arr[sec_oper[0] - 'A']; 
                                }
                            }
                        }
                    }

                    if(res == 1)
                        res_str = "TRUE";           //运算结果转换为字符串压栈 
                    else
                        res_str = "FALSE";
                    oper.push(res_str);
                    if(str==")" && sym.top()=="("){ //遇到左括号退出循环 
                        sym.pop();
                        break;
                    }
                }

                if(str != ")") 
                    sym.push(str);                  //str入栈 
            }
        }
        str = "";
        str_len = 0;
    }
    if(!oper.empty()){
        string lst = oper.top();
        if(lst == "TRUE")
            return 1;
        else if(lst == "FALSE")
            return 0;
        else
            return reg_arr[lst[0] - 'A'];   
    }
    else{
        printf("oper stack is empty!");
        return false;
    }
}

int get_pri(string str)
{
    int num = 6;
    string prim[num] = {"#", ")", "OR", "AND", "NOT", "("};
    for(int i=0; i<num; i++)
        if(prim[i] == str)
            return i;

    return -1;
}

bool is_fork()                  //判断当前点是否分叉 
{
    for(int i=0; i<M; i++)
        if(cur_poi.x == fork_point[i].x && cur_poi.y == fork_point[i].y)
            return true;

    return false; 
} 

void next_posi(int res, Point &poi)         //计算下一个位置 
{
    int dir = cur_poi.dir;                  //到当前点的方向 
    if(res == 1){                           //右拐 
        if(dir == 0){                       //原左 
            poi.x = cur_poi.x;
            poi.y = cur_poi.y+1;
            poi.dir = 2;
        }
        else if(dir == 1){                  //原右 
            poi.x = cur_poi.x;
            poi.y = cur_poi.y-1;
            poi.dir = 3;
        }
        else if(dir == 2){                  //原上 
            poi.x = cur_poi.x+1;
            poi.y = cur_poi.y; 
            poi.dir = 1;
        }
        else{                               //原下 
            poi.x = cur_poi.x-1;
            poi.y = cur_poi.y;
            poi.dir = 0;
        }
    }
    else if(res == 0){                      //左拐 
        if(dir == 0){
            poi.x = cur_poi.x;
            poi.y = cur_poi.y-1;
            poi.dir= 3;
        }
        else if(dir == 1){
            poi.x = cur_poi.x;
            poi.y = cur_poi.y+1;
            poi.dir = 2;
        }
        else if(dir == 2){
            poi.x = cur_poi.x-1;
            poi.y = cur_poi.y;
            poi.dir = 0;
        }
        else{
            poi.x = cur_poi.x+1;
            poi.y = cur_poi.y;
            poi.dir = 1;
        }
    }
    else{                                   //直走 
        if(dir == 0){
            poi.x = cur_poi.x-1;
            poi.y = cur_poi.y;
        }
        else if(dir == 1){
            poi.x = cur_poi.x+1;
            poi.y = cur_poi.y;
        }
        else if(dir == 2){
            poi.x = cur_poi.x;
            poi.y = cur_poi.y+1;
        }
        else{
            poi.x = cur_poi.x;
            poi.y = cur_poi.y-1;
        }
        poi.dir = dir;
    }
}

bool is_out(Point poi)                  //检查(x,y)是否出界 
{
    if(poi.x<-N || poi.x>N || poi.y<-N || poi.y>N)
        return true;
    else
        return false;
}

bool is_invert(int &reg)                //检查当前点是否转换点,若是,引用带回寄存器编号 
{
    for(int i=0; i<K; i++)
        if(cur_poi.x == inv[i].a && cur_poi.y == inv[i].b){
            reg = inv[i].c[0] - 'A';
            return true;
        }

    return false; 
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ava实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),可运行高分资源 Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现
C语言是一种广泛使用的编程语言,它具有高效、灵活、可移植性强等特点,被广泛应用于操作系统、嵌入式系统、数据库、编译器等领域的开发。C语言的基本语法包括变量、数据类型、运算符、控制结构(如if语句、循环语句等)、函数、指针等。下面详细介绍C语言的基本概念和语法。 1. 变量和数据类型 在C语言中,变量用于存储数据,数据类型用于定义变量的类型和范围。C语言支持多种数据类型,包括基本数据类型(如int、float、char等)和复合数据类型(如结构体、联合等)。 2. 运算符 C语言中常用的运算符包括算术运算符(如+、、、/等)、关系运算符(如==、!=、、=、<、<=等)、逻辑运算符(如&&、||、!等)。此外,还有位运算符(如&、|、^等)和指针运算符(如、等)。 3. 控制结构 C语言中常用的控制结构包括if语句、循环语句(如for、while等)和switch语句。通过这些控制结构,可以实现程序的分支、循环和多路选择等功能。 4. 函数 函数是C语言中用于封装代码的单元,可以实现代码的复用和模块化。C语言中定义函数使用关键字“void”或返回值类型(如int、float等),并通过“{”和“}”括起来的代码块来实现函数的功能。 5. 指针 指针是C语言中用于存储变量地址的变量。通过指针,可以实现对内存的间接访问和修改。C语言中定义指针使用星号()符号,指向数组、字符串和结构体等数据结构时,还需要注意数组名和字符串常量的特殊性质。 6. 数组和字符串 数组是C语言中用于存储同类型数据的结构,可以通过索引访问和修改数组中的元素。字符串是C语言中用于存储文本数据的特殊类型,通常以字符串常量的形式出现,用双引号("...")括起来,末尾自动添加'\0'字符。 7. 结构体和联合 结构体和联合是C语言中用于存储不同类型数据的复合数据类型。结构体由多个成员组成,每个成员可以是不同的数据类型;联合由多个变量组成,它们共用同一块内存空间。通过结构体和联合,可以实现数据的封装和抽象。 8. 文件操作 C语言中通过文件操作函数(如fopen、fclose、fread、fwrite等)实现对文件的读写操作。文件操作函数通常返回文件指针,用于表示打开的文件。通过文件指针,可以进行文件的定位、读写等操作。 总之,C语言是一种功能强大、灵活高效的编程语言,广泛应用于各种领域。掌握C语言的基本语法和数据结构,可以为编程学习和实践打下坚实的基础。
用代码解决这个问题The program committee of the school programming contests, which are often held at the Ural State University, is a big, joyful, and united team. In fact, they are so united that the time spent together at the university is not enough for them, so they often visit each other at their homes. In addition, they are quite athletic and like walking. Once the guardian of the traditions of the sports programming at the Ural State University decided that the members of the program committee spent too much time walking from home to home. They could have spent that time inventing and preparing new problems instead. To prove that, he wanted to calculate the average distance that the members of the program committee walked when they visited each other. The guardian took a map of Yekaterinburg, marked the houses of all the members of the program committee there, and wrote down their coordinates. However, there were so many coordinates that he wasn't able to solve that problem and asked for your help. The city of Yekaterinburg is a rectangle with the sides parallel to the coordinate axes. All the streets stretch from east to west or from north to south through the whole city, from one end to the other. The house of each member of the program committee is located strictly at the intersection of two orthogonal streets. It is known that all the members of the program committee walk only along the streets, because it is more pleasant to walk on sidewalks than on small courtyard paths. Of course, when walking from one house to another, they always choose the shortest way. All the members of the program committee visit each other equally often. Input The first line contains the number n of members of the program committee (2 ≤ n ≤ 105). The i-th of the following n lines contains space-separated coordinates xi, yi of the house of the i-th member of the program committee (1 ≤ xi, yi ≤ 106). All coordinates are integers. Output Output the average distance, rounded down to an integer, that a member of the program committee walks from his house to the house of his colleague.
05-26

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值