Shaass and Painter Robot : CodeForces294D
2017-06-29 20:45
225 查看
题目 Shaass and Painter Robot : CodeForces294D
题意
给你一张N*M(2 ≤ n, m ≤ 10^5)的矩形图和一个机器人。机器人一开始位于(x0,y0),它只能斜着朝一个方向走,当他走到某格时会将该格染成黑色,不管之前是否染过。每次将格子染成黑色需要消耗1单位的黑色染料。当机器人撞到墙壁时,会以镜面折射继续行走,注意,当机器人从另一块瓷砖进入瓷砖时,瓷砖会被粉刷,换句话说,在同一块瓷砖中改变方向不会导致任何绘画。现在已知N,M,x0,y0和机器人的朝向(UL左上,UR右上,DL左下,DR右下),问机器人将整个图染成黑白相间棋盘需要消耗多少染料,若无法完成输出-1。思路
首先,暴力模拟会超时,因为图比较大。仔细想想,其实如果整个图能染成黑白相间的话,当且仅当四条边界能够被染色成黑白相间。或者说四条边合起来有2*(N+M-2)个黑色方格。这样的话,只要模拟四条边的情况就可以了。代码比较短,自己理解吧。代码
#include<cstdio> #include<algorithm> using namespace std; int n,m,x,y,tot,a,b,w,f[4][100010],dx,dy; long long ans; char ch[10]; void doit(int x,int y){ if(x==1)a=0,b=y;else if(x==n)a=1,b=y;else if(y==1)a=2,b=x;else a=3,b=x; if(!f[a][b])f[a][b]=1,tot++; if(x==1)dx=1;if(x==n)dx=-1;if(y==1)dy=1;if(y==m)dy=-1; } int main(){ scanf("%d%d%d%d%s",&n,&m,&x,&y,ch); ch[0]=='D'?dx=1:dx=-1;ch[1]=='R'?dy=1:dy=-1;ans=1; for(int i=1;i<=2*(n+m-2);i++){ doit(x,y); if(tot>=n+m-2)return printf("%I64d",ans),0; w=min(dx>0?n-x:x-1,dy>0?m-y:y-1); ans+=w;x+=w*dx;y+=dy*w; } return printf("-1"),0; }
相关文章推荐
- [CF 294D]Shaass and Painter Robot解题报告
- Codeforces Round #206 (Div. 1) A. Vasya and Robot
- 数学 之 Codeforces 354A - Vasya and Robot
- Install RL(Robotcs Library) on ubuntu and using
- Codeforces294B - Shaass and Bookshelf(贪心)
- Robot Framework and Ride
- selenium webdriver 右键另存为下载文件(结合robot and autoIt)
- CodeForces 294C - Shaass and Lights 统计
- Codeforces Round #178 (Div. 2) C. Shaass and Lights 【组合数学】
- A. Shaass and Oskols
- CodeForces 321 A - Ciel and Robot
- Codeforces Round #190 (Div. 2)-C. Ciel and Robot
- [CF355C]Vasya and Robot(思维,贪心)
- 数学 之 Codeforces 354A - Vasya and Robot
- Robot and Clipboard
- CodeForces 354A - Vasya and Robot 简单思维..赋初值一定不能随便了T_T
- How to distinguish a human and a robot in the future?
- Robot Framework and Ride
- codeforces A. Shaass and Oskols 题解
- CF 321A(Ciel and Robot-暴力枚举)