社区讨论
提供 SPJ
P2493[SDOI2011] 贪食蛇参与者 2已保存回复 2
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 2 条
- 当前快照
- 1 份
- 快照标识符
- @lwlrkpiz
- 此快照首次捕获于
- 2024/05/25 15:03 2 年前
- 此快照最后确认于
- 2024/05/25 17:00 2 年前
CPP
#include "testlib.h"
#include <bits/stdc++.h>
constexpr int dx[]={-1,1,0,0},dy[]={0,0,-1,1};
int r,c,n,cost,realcost,w[17][17],f[17][17];
int food;
std::string path;
std::deque<std::pair<int,int> > snake;
inline int getdir(char c){
switch(c){
case 'U':return 0;
case 'D':return 1;
case 'L':return 2;
case 'R':return 3;
}
return -1;
}
int main(int argc, char *argv[]) {
registerTestlibCmd(argc,argv);
std::string jans = ans.readLine(),
pans = ouf.readLine();
while(!jans.empty()&&isspace(jans.back())) jans.pop_back();
while(!pans.empty()&&isspace(pans.back())) pans.pop_back();
if(pans!=jans) quitf(_wa,"The first line is wrong. Read '%s', expected '%s'.",pans.c_str(),jans.c_str());
if(pans!="No solution."){
cost=std::stoi(pans);
if(cost>0){
path=ouf.readToken();
r=inf.readInt(),c=inf.readInt();
if(path.size()>20000000u) quitf(_wa,"The path is too long.");
for(int i{1};i<=r;++i){
std::string _=inf.readToken();
for(int j{1};j<=c;++j)
w[i][j]=(_[j-1]&15);
}
for(int i{0};i<4;++i){
int x=inf.readInt(),y=inf.readInt();
snake.emplace_back(x,y);
}
n=inf.readInt();
for(int i{0};i<n;++i){
int x=inf.readInt(),y=inf.readInt();
f[x][y]|=(1<<i);
}
for(int i{0};i<(int)path.size();++i){
char c{path[i]};
int d=getdir(c);
if(d==-1)
quitf(_wa,"On step %d, unknown direction character '%c'.\n",i+1,c);
auto [x,y]=snake.front();
int tx{x+dx[d]},ty{y+dy[d]};
if(!(tx>=1&&tx<=r&&ty>=1&&ty<=c)) quitf(_wa,"The snake walked out of the map, on step %d.",i+1);
if(!w[tx][ty]) quitf(_wa,"The snake walked on (%d,%d) whose W is 0, on step %d.",tx,ty,i+1);
food|=f[tx][ty];
snake.emplace_front(tx,ty);
if((int)snake.size()>4+__builtin_popcount(food)) snake.pop_back();
for(int j{1};j<(int)snake.size();++j)
if(snake[0]==snake[j])
quitf(_wa,"The snake walked on itself, on step %d.",i+1);
realcost+=std::abs(w[x][y]-w[tx][ty])+1;
}
if(food!=(1<<n)-1)
quitf(_wa,"The snake hasn't eaten all food.");
if(realcost!=cost)
quitf(_wa,"The cost of your path is %d, expected %d.",realcost,cost);
}
quitf(_ok,"Accepted. cost = %d.",cost);
}
else
quitf(_ok,"Accepted. No solution.");
return 0;
}
回复
共 2 条回复,欢迎继续交流。
正在加载回复...