这两个题,讲的是有一种奇怪的语言,代码是一种二维的矩阵。

前一个题,是根据所给的要求,写一个简单的解释器。

后一个题,是用那种语言写一个简单的小程序。

挺有意思的,所以在这里纪念一下。顺便那个语言的原型是一种叫做Befunge的语言。真实存在的哟。

代码:

  简单的解释器:  

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <cctype>
#include <time.h> using namespace std; const int INF = <<;
const int MAXN = ;
const int MAXM = 1e5+;
const int MAXSTEP = 1e6;
const int MAX_NUM = 1e5;
const int dir[][] = { {, }, {, }, {, -}, {-, } }; int H, W;
char G[MAXN][MAXN];
int num[MAXM], n, curNum;
int pointer[], d;
int stepCnt; int cur, memory[]; bool check() {
if (!(-MAX_NUM<=cur&&cur<=MAX_NUM)) {
//溢出错误
puts("OVERFLOW ERROR");
return false;
}
stepCnt++;
if (stepCnt>=MAXSTEP) {
//超时
puts("TIME LIMIT EXCEEDED");
return false;
}
pointer[] += dir[d][]; pointer[] += dir[d][];
if (!(<=pointer[]&&pointer[]<H && <=pointer[]&&pointer[]<W)) {
//指针越界
puts("RUNTIME ERROR");
return false;
}
if (curNum>=n) curNum = n-;
return true;
} void init() {
//初始化寄存器
cur = ;
memset(memory, , sizeof(memory));
//初始化指针
pointer[] = pointer[] = ;
d = ;
//初始化计数器
stepCnt = ;
curNum = ;
} void run() {
init();
while (true) {
char c = G[pointer[]][pointer[]];
if ('#'==c)
break;
else if ('^'==c)
d = ;
else if ('v'==c)
d = ;
else if ('<'==c)
d = ;
else if ('>'==c)
d = ;
else if ('@'==c)
if (cur!=) d = (d+)%;
else d = (d+)%;
else if ('A'<=c&&c<='Z')
swap(cur, memory[c-'A']);
else if ('?'==c) {
cur = num[curNum++];
} else if ('!'==c) {
printf("%d\n", cur);
cur = ;
} else if ('+'==c)
cur++;
else if ('-'==c)
cur--;
else if ('.'==c)
;
if (!check()) break;
}
} int main() {
#ifdef Phantom01
freopen("URAL2027.txt", "r", stdin);
#endif //Phantom01 while (scanf("%d%d", &H, &W)!=EOF) {
for (int i = ; i < H; i++)
scanf("%s", G[i]);
scanf("%d", &n);
for (int i = ; i < n; i++)
scanf("%d", &num[i]);
run();
} return ;
}

解释器

  小程序:

 9 12
?..........v
>.>v>>Tv>>v.
.>.>@..>@^I.
.-..I..^A.-.
.^..+..^+.v<
.T..I..-A...
.^+T<..^<...
^.........@v
........#!A<

小程序

URAL 2027 2028 两个有趣的题的更多相关文章

  1. Ural 1209. 1, 10, 100, 1000... 一道有趣的题

    1209. 1, 10, 100, 1000... Time limit: 1.0 secondMemory limit: 64 MB Let's consider an infinite seque ...

  2. URAL - 1917 Titan Ruins: Deadly Accuracy(水题)

    水题一个,代码挫了一下: 题意不好理解. 你去一个洞窟内探险,洞窟内有许多宝石,但都有魔法守护,你需要用魔法将它们打下来. 每个宝石都有自己的防御等级,当你的魔法超过它的防御等级时它就会被你打下来. ...

  3. 两道相似KMP题

    1.POJ 3450 Coporate Identity 这两题的解法都是枚举子串,然后匹配,像这种题目以后可以不用KMP来做,直接字符串自带的strstr函数搞定,如果字符串未出现,该函数返回NUL ...

  4. URAL 1008 - Image Encoding(bfs坑爹题)

    坑爹题,两种输入输出互相交换,裸bfs #include <stdio.h> #include <string.h> typedef struct { int x; int y ...

  5. 又一道简单题&&Ladygod(两道思维水题)

    Ladygod Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit S ...

  6. 一道cf水题再加两道紫薯题的感悟

    . 遇到一个很大的数除以另一个数时,可以尝试把这个很大的数进行,素数因子分解. . 遇到多个数的乘积与另一个数的除法时,求是否能整除,可以先求每一个数与分母的最大公约数,最后若分母数字为1,则证明可整 ...

  7. bzoj1814 Ural 1519 Formula 1(插头dp模板题)

    1814: Ural 1519 Formula 1 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 924  Solved: 351[Submit][Sta ...

  8. FJOI2020 的两道组合计数题

    最近细品了 FJOI2020 的两道计数题,感觉抛开数据范围不清还卡常不谈里面的组合计数技巧还是挺不错的.由于这两道题都基于卡特兰数的拓展,所以我们把它们一并研究掉. 首先是 D1T3 ,先给出简要题 ...

  9. 发现两个有趣的CSS3效果

    一.CSS3画机器猫 http://keleyi.com/keleyi/phtml/html5/3.htm 哆啦A梦效果图: 可用于浏览器对CSS3支持情况的测试 但最近有人对这个测试表示怀疑,指该测 ...

随机推荐

  1. LD_DEBUG

    LD_DEBUG 是 glibc 中的 loader 为了方便自身调试而设置的一个环境变量.通过设置这个环境变量,可以方便的看到 loader 的加载过程. LD_DEBUG=help ./main ...

  2. BZOJ 3672 [NOI2014]购票 (凸优化+树剖/树分治)

    题目大意: 略 题面传送门 怎么看也是一道$duliu$题= = 先推式子,设$dp[x]$表示到达$x$点到达1节点的最小花费 设$y$是$x$的一个祖先,则$dp[x]=min(dp[y]+(di ...

  3. Android开发新手HelloWorld解析

    首先看这个 HelloWorld 类. Java代码public class HelloWorld extends Activity {       /** Called when the activ ...

  4. tp框架报错 Namespace declaration statement has to be the very first statement in the script

    Namespace declaration statement has to be the very first statement in the script tp框架报这个错误,错误行数就是nam ...

  5. ASP.NET-Razor常用方法

    1.使用Scripts.Render()引入脚本 @sectionScrits{ @Scripts.Render("~/bundles/jquery") } 2.使用@Html.H ...

  6. ASP.NET-HTTP响应标头

    Reponse Headers 理论上所有的响应头信息都应该是回应请求头的.但是服务端为了效率,安全,还有其他方面的考虑,会添加相对应的响应头信息,从上图可以看到: Cache-Control:mus ...

  7. Qt程序打包成exe可执行文件

    很多Qt爱好者想发布自己的Qt软件,但却发现在其他没有安装Qt SDK的机器上无法运行,这就是本文想要说明的问题.现在网上大部分软件都要发布自己开发的应用程序,都会打包到exe文件中,待安装完exe文 ...

  8. java发送邮件带附件

    package com.smtp; import java.util.Vector; public class MailBean { private String to; // 收件人 private ...

  9. JavaScript DOM(一)

    文件夹: DOM家谱树 节点的基本属性和方法 document与getElement方法 DOM家谱树 DOM能够将不论什么HTML或XML文档描绘成一个由多层次节点构成的结构.当中节点被分为几种不同 ...

  10. Hadoop实战:使用Combiner提高Map/Reduce程序效率

    好不easy算法搞定了.小数据測试也得到了非常好的结果,但是扔到进群上.挂上大数据就挂了.无休止的reduce不会结束了. .. .. .... .. ... .. ================= ...