Hướng dẫn giải của Dãy lệnh điều khiển
Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này
Code mẫu của flashmt
const dx:array[0..3] of longint=(-1,0,1,0); dy:array[0..3] of longint=(0,1,0,-1); var s,a:ansistring; x,y,d,i,j:longint; begin while true do begin readln(s); if s='[END]' then break; if s='[CASE]' then begin readln(s); a:=''; d:=0; x:=0; y:=0; while true do begin readln(s); if s='>>' then break; a:=a+s; end; for j:=1 to 4 do for i:=1 to length(a) do begin if a[i]='L' then d:=(d+3) and 3 else if a[i]='R' then d:=(d+1) and 3 else begin x:=x+dx[d]; y:=y+dy[d]; end; end; if x*x+y*y<>0 then write('un'); writeln('bounded'); end; end; end.
Code mẫu của ladpro98
#include <bits/stdc++.h> using namespace std; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, 1, 0, -1}; int x, y, d, i; string a, s, st; int main() { while (true) { getline(cin, st); if (st == "[END]") break; a = ""; getline(cin, st); while (true) { getline(cin, s); if (s == ">>") break; a = a + s; } scanf("\n"); x = 0; y = 0; d = 0; for(i=0; i<a.length(); i++) { if (a[i] == 'L') d = (d + 3) % 4; if (a[i] == 'R') d = (d + 1) % 4; if (a[i] == 'S') { x += dx[d]; y += dy[d]; } } if (d == 0 && (x != 0 || y != 0)) printf("unbounded\n"); else printf("bounded\n"); } return 0; }
Code mẫu của RR
#include <iostream> #include <algorithm> #define FOR(i,a,b) for(int i=a; i<=b; i++) #define FORD(i,a,b) for(int i=a; i>=b; i--) using namespace std; const int di[4]={-1,0,1,0}; const int dj[4]={0,1,0,-1}; int n; char a[55][55]; char s[55]; int main() { cin >>s; while (s[1]!='E') { cin >>s; n=0; while (true) { n++; cin >>a[n]; if (a[n][0]=='>') { n--; break; } } int dir=0, x=0, y=0; FOR(turn,1,4) { FOR(i,1,n) FOR(j,0,strlen(a[i])) { if (a[i][j]=='S') { x+=di[dir]; y+=dj[dir]; } else if (a[i][j]=='L') { dir++; if (dir>=4) dir-=4; } else if (a[i][j]=='R') { dir--; if (dir<0) dir+=4; } } } if (x || y) printf("unbounded\n"); else printf("bounded\n"); cin >>s; } return 0; }
Code mẫu của hieult
#include <stdio.h> #include <string.h> //#include <conio.h> int main() { //freopen("COMMAND.inp","r",stdin); char ss[10]; while(gets(ss)>0) { if(ss[0]!='[') continue; else if(ss[1]=='E') break; else { char s[55]; int x=0,y=0,huong =1; gets(s); while(gets(s)>0&&s[0]!='>') { for(int i=0;i<strlen(s);i++) { if(s[i]=='S') { if(huong==1) y++; if(huong==2) x++; if(huong==3) y--; if(huong==4) x--; } else if(s[i]=='R') huong++; else if(s[i]=='L') huong--; if(huong == 0) huong = 4; if(huong == 5) huong = 1; } } // printf("\n*** %d %d %d ***",x,y,huong); if((x==0&&y==0)||huong!=1) printf("bounded\n"); else printf("unbounded\n"); } } // getch(); }
Code mẫu của ll931110
#include <algorithm> #include <bitset> #include <cmath> #include <cstring> #include <deque> #include <fstream> #include <iostream> #include <iterator> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> typedef long long ll; using namespace std; int dx[4] = {-1,0,1,0}; int dy[4] = {0,-1,0,1}; vector<string> a; int main() { // freopen("command.in","r",stdin); // freopen("command.ou","w",stdout); string s; while (1) { cin >> s; if (s == "[END]") break; cin >> s; a.clear(); while (1) { cin >> s; if (s == ">>") break; a.push_back(s); }; int sx = 0,sy = 0,dir = 0; for (int i = 0; i < a.size(); i++) for (int j = 0; j < a[i].size(); j++) if (a[i][j] == 'S') { sx += dx[dir]; sy += dy[dir]; } else if (a[i][j] == 'L') dir = (dir + 3) % 4; else dir = (dir + 1) % 4; if ((!sx && !sy) || dir) printf("bounded\n"); else printf("unbounded\n"); }; };
Bình luận