【绿书】|【绿书】 模拟,rep大坑

https://vjudge.net/contest/229603#problem/B
绿书题
大模拟,绿书上用了个比较麻烦的输入,其实只要getchar()!='0'就行
坑:
rep(i,0,s.length()-1)会wa
len=s.length()-1后不会。。。

#define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#define INF 0x3f3f3f3f #define eps 1e-6 #define pi acos(-1.0) #define e exp(1.0) #define rep(i,t,n)for(int i =(t); i<=(n); ++i) #define per(i,n,t)for(int i =(n); i>=(t); --i) #define mp make_pair #define pb push_back #define mmm(a,b) memset(a,b,sizeof(a)) //std::ios::sync_with_stdio(false); using namespace std; typedef long long ll; typedef unsigned long long ull; void smain(); #define ONLINE_JUDGE int main() { //ios::sync_with_stdio(false); #ifndef ONLINE_JUDGE FILE *myfile; myfile =freopen("C:\\Users\\SuuTT\\Desktop\\test\\in.txt", "r", stdin); if (myfile == NULL) fprintf(stdout, "error on input freopen\n"); /*FILE *outfile; outfile= freopen("C:\\Users\\SuuTT\\Desktop\\test\\out.txt", "w", stdout); if (outfile == NULL) fprintf(stdout, "error on output freopen\n"); */ long _begin_time = clock(); #endif smain(); #ifndef ONLINE_JUDGE long _end_time = clock(); printf("time = %ld ms.", _end_time - _begin_time); #endif return 0; } int dir[4][2] = { 1,0,0,1,-1,0,0,-1 }; const int maxn = 3e3 + 5; struct point { int x, y; point(int x = 0, int y = 0) :x(x), y(y) {}}; typedef point Vector; Vector operator+ (const Vector& A, const Vector& B) { return Vector(A.x + B.x, A.y + B.y); } Vector operator- (const point& A, const point& B) { return Vector(A.x - B.x, A.y - B.y); } Vector operator* (const Vector& A, int p) { return Vector(A.x*p, A.y*p); } Vector operator/ (const Vector& A, int p) { return Vector(A.x / p, A.y / p); } bool operator== (const point& a, const point &b) { return a.x == b.x && a.y == b.y; } bool operator< (const point& p1, const point& p2) { return p1.x < p2.x || (p1.x == p2.x && p1.y < p2.y); } const int Gsize = 5; vector grid; point ePos; map Dirs; bool valid(const point& p) { return p.x >= 0 && p.x < Gsize&&p.y >= 0 && p.y < Gsize; } voidprintGrid() { for (int i = 0; i < Gsize; i++) { rep(j, 0, Gsize - 1) { if (j)cout << ' '; cout << grid[i][j]; } cout << endl; } } bool tryMove(char cmd) { if (!Dirs.count(cmd))return false; assert(Dirs.count(cmd)); point p = ePos + Dirs[cmd]; if (!valid(p))return false; swap(grid[p.x][p.y], grid[ePos.x][ePos.y]); ePos = p; return true; } int n; int ans; void Run(){}void smain() { int t=1; string line; Dirs['A'] = Vector(-1, 0); Dirs['B'] = Vector(1, 0); Dirs['L'] = Vector(0, -1); Dirs['R'] = Vector(0, 1); while (1) { grid.clear(); ePos.x = -1, ePos.y = -1; rep(i, 0, Gsize - 1) { getline(cin, line); if (line == "Z")return; assert(line.size() == Gsize); rep(j, 0, Gsize - 1){ if (line[j] != ' ')continue; assert(ePos.x == -1 && ePos.y == -1); ePos.x = i; ePos.y = j; } grid.push_back(line); } //char move; string moves; while (1) { getline(cin, line); assert(!line.empty()); bool end = *(line.rbegin()) == '0'; if (!end) moves.append(line); else moves.append(line, 0, line.size() - 1); if (end)break; } bool legal = 1; //int len =; //rep(i, 0, moves.size() - 1)if (!tryMove(moves[i])) { legal = false; break; } for (const auto& m : moves) if (!tryMove(m)) { legal = false; break; } if (t > 1)cout << endl; cout << "Puzzle #" << t++ << ":" << endl; //Puzzle #2: if (legal) printGrid(); else cout << "This puzzle has no final configuration." << endl; }} /*TRGSJ XDOKI M VLN WPABE UQHCF ARRBBL0 ABCDE FGHIJ KLMNO PQRS TUVWX AAA LLLL0 ABCDE FGHIJ KLMNO PQRS TUVWX AAAAABBRRRLL0 Z*/


【【绿书】|【绿书】 模拟,rep大坑】转载于:https://www.cnblogs.com/SuuT/p/9051885.html

    推荐阅读