output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include<iostream> #include<algorithm> #include<map> #include<stack> using namespace std; #define INF 100000000 typedef pair<string,int> P; int x,y; string f; char c, alph[] = {'R','G','B'}; int ans; int r,g,b; map<string,int> memo; int dy[] = {-1,0,1,0}, dx[] = {0,1,0,-1}; bool vis[10][10]; void color(string &s,int h,int w, char lu, char nxt){ s[h*x+w] = nxt; for(int i=0;i<4;i++){ int ty = h+dy[i], tx = w+dx[i]; if(ty<0 || tx<0 || y<=ty || x<=tx)continue; if(s[ty*x + tx] != lu)continue; color(s,ty,tx,lu,nxt); } } int count(string s,int h,int w, char lu){ if(vis[h][w])return 0; vis[h][w] = true; int res = 1; for(int i=0;i<4;i++){ int ty = h+dy[i], tx = w+dx[i]; if(ty<0 || tx<0 || y<=ty || x<=tx)continue; if(s[ty*x + tx] != lu)continue; res += count(s,ty,tx,lu); } return res; } int rec(string field,int depth){ if(ans<depth)return INF; if(memo.find(field) != memo.end()){ ans = min(ans,depth+memo[field]); return memo[field]; } for(int j=0;j<y;j++)for(int k=0;k<x;k++)vis[j][k] = false; int mount = count(field,0,0,field[0]); if(mount == y*x){ ans = min(ans,depth); return memo[field] = 0; } string G[2]; int num[2],cnt=0; for(int i=0;i<3;i++){ G[cnt] = field; if(alph[i] != G[cnt][0]){ color(G[cnt],0,0,G[cnt][0],alph[i]); for(int j=0;j<y;j++)for(int k=0;k<x;k++)vis[j][k] = false; num[cnt] = count(G[cnt],0,0,alph[i]); cnt++; } if(cnt>=2)break; } if(num[0] < num[1]){ swap(num[0],num[1]); swap(G[0],G[1]); } if(num[0] == y*x){ ans = min(ans,depth+1); return memo[field] = 1; }else{ int tmp = rec(G[0],depth+1); if(mount != num[1])tmp = min(tmp,rec(G[1],depth+1)); ans = min(ans,depth+1+tmp); return memo[field] = tmp+1; } } int main(){ while(cin >> x >> y,x||y){ f = ""; for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ cin >> c; f += c; } } memo.clear(); ans = INF; cout << rec(f,0) << endl; } }
### Prompt Your challenge is to write a cpp solution to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include<iostream> #include<algorithm> #include<map> #include<stack> using namespace std; #define INF 100000000 typedef pair<string,int> P; int x,y; string f; char c, alph[] = {'R','G','B'}; int ans; int r,g,b; map<string,int> memo; int dy[] = {-1,0,1,0}, dx[] = {0,1,0,-1}; bool vis[10][10]; void color(string &s,int h,int w, char lu, char nxt){ s[h*x+w] = nxt; for(int i=0;i<4;i++){ int ty = h+dy[i], tx = w+dx[i]; if(ty<0 || tx<0 || y<=ty || x<=tx)continue; if(s[ty*x + tx] != lu)continue; color(s,ty,tx,lu,nxt); } } int count(string s,int h,int w, char lu){ if(vis[h][w])return 0; vis[h][w] = true; int res = 1; for(int i=0;i<4;i++){ int ty = h+dy[i], tx = w+dx[i]; if(ty<0 || tx<0 || y<=ty || x<=tx)continue; if(s[ty*x + tx] != lu)continue; res += count(s,ty,tx,lu); } return res; } int rec(string field,int depth){ if(ans<depth)return INF; if(memo.find(field) != memo.end()){ ans = min(ans,depth+memo[field]); return memo[field]; } for(int j=0;j<y;j++)for(int k=0;k<x;k++)vis[j][k] = false; int mount = count(field,0,0,field[0]); if(mount == y*x){ ans = min(ans,depth); return memo[field] = 0; } string G[2]; int num[2],cnt=0; for(int i=0;i<3;i++){ G[cnt] = field; if(alph[i] != G[cnt][0]){ color(G[cnt],0,0,G[cnt][0],alph[i]); for(int j=0;j<y;j++)for(int k=0;k<x;k++)vis[j][k] = false; num[cnt] = count(G[cnt],0,0,alph[i]); cnt++; } if(cnt>=2)break; } if(num[0] < num[1]){ swap(num[0],num[1]); swap(G[0],G[1]); } if(num[0] == y*x){ ans = min(ans,depth+1); return memo[field] = 1; }else{ int tmp = rec(G[0],depth+1); if(mount != num[1])tmp = min(tmp,rec(G[1],depth+1)); ans = min(ans,depth+1+tmp); return memo[field] = tmp+1; } } int main(){ while(cin >> x >> y,x||y){ f = ""; for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ cin >> c; f += c; } } memo.clear(); ans = INF; cout << rec(f,0) << endl; } } ```
#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <complex> #include <string> #include <sstream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <functional> #include <iostream> #include <map> #include <set> using namespace std; typedef pair<int,int> P; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector< vector<char> > F; #define pu push #define pb push_back #define mp make_pair #define eps 1e-9 #define INF 2000000000 #define sz(x) ((int)(x).size()) #define fi first #define sec second #define SORT(x) sort((x).begin(),(x).end()) #define all(x) (x).begin(),(x).end() #define EQ(a,b) (abs((a)-(b))<eps) struct Fi { int turn; char f[10][10]; Fi(int t,char b[10][10]) { turn=t; for(int i=0;i<10;i++) { for(int j=0;j<10;j++) { f[i][j]=b[i][j]; } } } }; int n,m; char rgb[4]="RGB"; bool All(char a[10][10]) { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(a[i][j]!=a[0][0])return false; } } return true; } int dx[4]={0,0,1,-1}; int dy[4]={1,-1,0,0}; char buf[10][10]; char st[10][10]; bool used[10][10]; char sor; int ty; void print(char a[10][10]) { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { cout << a[i][j] << ' '; } cout << endl; } return; } void dfs(int x,int y) { //cout << x << ' ' << y << endl; used[x][y]=true; buf[x][y]=rgb[ty]; for(int i=0;i<4;i++) { int nx=x+dx[i],ny=y+dy[i]; if(nx<0||nx>=n||ny<0||ny>=m)continue; if(used[nx][ny])continue; if(buf[nx][ny]!=sor)continue; dfs(nx,ny); } return; } int bfs() { if(All(st))return 0; queue<Fi> q; Fi start(0,st); q.push(start); while(!q.empty()) { Fi a=q.front(); //print(a.f); q.pop(); for(int i=0;i<3;i++) { if((a.f)[0][0]==rgb[i])continue; memset(used,false,sizeof(used)); sor=(a.f)[0][0]; ty=i; memcpy(buf,a.f,sizeof(a.f)); dfs(0,0); //print(nb); //cout << a.fi+1 << endl << endl; if(All(buf))return a.turn+1; q.push(Fi(a.turn+1,buf)); } } return 0; } int main() { while(1) { cin >> m >> n; if(n==0&&m==0)break; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { cin >> st[i][j]; } } cout << bfs() << endl; } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <complex> #include <string> #include <sstream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <functional> #include <iostream> #include <map> #include <set> using namespace std; typedef pair<int,int> P; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector< vector<char> > F; #define pu push #define pb push_back #define mp make_pair #define eps 1e-9 #define INF 2000000000 #define sz(x) ((int)(x).size()) #define fi first #define sec second #define SORT(x) sort((x).begin(),(x).end()) #define all(x) (x).begin(),(x).end() #define EQ(a,b) (abs((a)-(b))<eps) struct Fi { int turn; char f[10][10]; Fi(int t,char b[10][10]) { turn=t; for(int i=0;i<10;i++) { for(int j=0;j<10;j++) { f[i][j]=b[i][j]; } } } }; int n,m; char rgb[4]="RGB"; bool All(char a[10][10]) { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(a[i][j]!=a[0][0])return false; } } return true; } int dx[4]={0,0,1,-1}; int dy[4]={1,-1,0,0}; char buf[10][10]; char st[10][10]; bool used[10][10]; char sor; int ty; void print(char a[10][10]) { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { cout << a[i][j] << ' '; } cout << endl; } return; } void dfs(int x,int y) { //cout << x << ' ' << y << endl; used[x][y]=true; buf[x][y]=rgb[ty]; for(int i=0;i<4;i++) { int nx=x+dx[i],ny=y+dy[i]; if(nx<0||nx>=n||ny<0||ny>=m)continue; if(used[nx][ny])continue; if(buf[nx][ny]!=sor)continue; dfs(nx,ny); } return; } int bfs() { if(All(st))return 0; queue<Fi> q; Fi start(0,st); q.push(start); while(!q.empty()) { Fi a=q.front(); //print(a.f); q.pop(); for(int i=0;i<3;i++) { if((a.f)[0][0]==rgb[i])continue; memset(used,false,sizeof(used)); sor=(a.f)[0][0]; ty=i; memcpy(buf,a.f,sizeof(a.f)); dfs(0,0); //print(nb); //cout << a.fi+1 << endl << endl; if(All(buf))return a.turn+1; q.push(Fi(a.turn+1,buf)); } } return 0; } int main() { while(1) { cin >> m >> n; if(n==0&&m==0)break; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { cin >> st[i][j]; } } cout << bfs() << endl; } return 0; } ```
#include <stdio.h> #include <queue> using namespace std; int tx[4] = {0,0,1,-1},ty[4] = {1,-1,0,0}; int x,y; class gamen{ public: char map[12][12]; int howcnt; int i,j; gamen(){ howcnt = 0; for(i = 0; i <= x+1; i++){ map[0][i] = 'L'; map[y+1][i] = 'L'; } for(i = 0; i <= y+1; i++){ map[i][0] = 'L'; map[i][x+1] = 'L'; } } void colorcenge(char iro,int nx,int ny){ int i,j; char temp; temp = map[ny][nx]; map[ny][nx] = iro; for(i = 0; i < 4; i++){ if(map[ny+ty[i]][nx+tx[i]] == temp){ colorcenge(iro,nx+tx[i],ny+ty[i]); } } } bool allsame(){ int i,j; char key = map[1][1]; for(i = 1; i <= y; i++){ for(j = 1; j <= x; j++){ if(map[i][j] != key){ return false; } } } return true; } void scan(){ for(i = 1; i <= y; i++){ for(j = 1; j <= x; j++){ scanf("%c",&map[i][j]); if(map[i][j] == ' ' || map[i][j] == '\n'){ j--; } } } } void print(){ int i,j; printf("count:%d\n",howcnt); for(i = 0; i <= y+1; i++){ for(j = 0; j <= x+1; j++){ printf("%c",map[i][j]); } printf("\n"); } } }; int main() { int i,j; char color[] = {"RGB"}; queue<gamen> que; gamen now; gamen foo; scanf("%d%d",&x,&y); while(x != 0 && y != 0){ while(!que.empty()) que.pop(); gamen men; men.scan(); que.push(men); while(!que.empty()){ now = que.front(); if(now.allsame()) break; now.howcnt++; for(i = 0; i < 3; i++){ if(color[i] != now.map[1][1]){ foo = now; foo.colorcenge(color[i],1,1); que.push(foo); } } que.pop(); } printf("%d\n",now.howcnt); scanf("%d%d",&x,&y); } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <stdio.h> #include <queue> using namespace std; int tx[4] = {0,0,1,-1},ty[4] = {1,-1,0,0}; int x,y; class gamen{ public: char map[12][12]; int howcnt; int i,j; gamen(){ howcnt = 0; for(i = 0; i <= x+1; i++){ map[0][i] = 'L'; map[y+1][i] = 'L'; } for(i = 0; i <= y+1; i++){ map[i][0] = 'L'; map[i][x+1] = 'L'; } } void colorcenge(char iro,int nx,int ny){ int i,j; char temp; temp = map[ny][nx]; map[ny][nx] = iro; for(i = 0; i < 4; i++){ if(map[ny+ty[i]][nx+tx[i]] == temp){ colorcenge(iro,nx+tx[i],ny+ty[i]); } } } bool allsame(){ int i,j; char key = map[1][1]; for(i = 1; i <= y; i++){ for(j = 1; j <= x; j++){ if(map[i][j] != key){ return false; } } } return true; } void scan(){ for(i = 1; i <= y; i++){ for(j = 1; j <= x; j++){ scanf("%c",&map[i][j]); if(map[i][j] == ' ' || map[i][j] == '\n'){ j--; } } } } void print(){ int i,j; printf("count:%d\n",howcnt); for(i = 0; i <= y+1; i++){ for(j = 0; j <= x+1; j++){ printf("%c",map[i][j]); } printf("\n"); } } }; int main() { int i,j; char color[] = {"RGB"}; queue<gamen> que; gamen now; gamen foo; scanf("%d%d",&x,&y); while(x != 0 && y != 0){ while(!que.empty()) que.pop(); gamen men; men.scan(); que.push(men); while(!que.empty()){ now = que.front(); if(now.allsame()) break; now.howcnt++; for(i = 0; i < 3; i++){ if(color[i] != now.map[1][1]){ foo = now; foo.colorcenge(color[i],1,1); que.push(foo); } } que.pop(); } printf("%d\n",now.howcnt); scanf("%d%d",&x,&y); } return 0; } ```
#include <iostream> #include <vector> #include <queue> #include <string> using namespace std; int x, y; void solve(int i, int j, vector<int>& v, int ch, int nx) { int dxy[] ={0, 1, 0, -1, 0}; for(int k = 0; k < 4; k++) { if(i+dxy[k] >= 0 && i+dxy[k] < y && j+dxy[k+1] >= 0 && j+dxy[k+1] < x) { if(((v[i+dxy[k]] >> (2*(j+dxy[k+1]))) & 3) == ch) { v[i+dxy[k]] &= ~(3 << (2*(j+dxy[k+1]))); v[i+dxy[k]] |= nx << (2*(j+dxy[k+1])); solve(i+dxy[k], j+dxy[k+1], v, ch, nx); } } } } int main() { while(cin >> x >> y, x || y) { vector<int> v; for(int i = 0; i < y; i++) { int a = 0; for(int j = 0; j < x; j++) { string c; cin >> c; if(c[0] == 'R') { a |= 0 << (2*j); } else if(c[0] == 'G') { a |= 1 << (2*j); } else { a |= 2 << (2*j); } } v.push_back(a); } queue<vector<int> > q; q.push(v); int ans = -1; for(int t = 0;; t++) { queue<vector<int> > nex; while(!q.empty()) { vector<int> vn[2]; vn[0] = vn[1] = q.front(); q.pop(); bool flg = true; for(int i = 0; i < y; i++) { for(int j = 0; j < x; j++) { if(((vn[0][i] >> (2*j)) & 3) != (vn[0][0] & 3)) flg = false; } } if(flg) { ans = t; goto exit; } int ch = vn[0][0] & 3; vn[0][0] &= ~3; vn[0][0] |= (ch+1)%3; solve(0,0,vn[0],ch,(ch+1)%3); vn[1][0] &= ~3; vn[1][0] |= (ch+2)%3; solve(0,0,vn[1],ch,(ch+2)%3); nex.push(vn[0]); nex.push(vn[1]); } q = nex; } exit:; cout << ans << endl; } }
### Prompt Please provide a Cpp coded solution to the problem described below: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <iostream> #include <vector> #include <queue> #include <string> using namespace std; int x, y; void solve(int i, int j, vector<int>& v, int ch, int nx) { int dxy[] ={0, 1, 0, -1, 0}; for(int k = 0; k < 4; k++) { if(i+dxy[k] >= 0 && i+dxy[k] < y && j+dxy[k+1] >= 0 && j+dxy[k+1] < x) { if(((v[i+dxy[k]] >> (2*(j+dxy[k+1]))) & 3) == ch) { v[i+dxy[k]] &= ~(3 << (2*(j+dxy[k+1]))); v[i+dxy[k]] |= nx << (2*(j+dxy[k+1])); solve(i+dxy[k], j+dxy[k+1], v, ch, nx); } } } } int main() { while(cin >> x >> y, x || y) { vector<int> v; for(int i = 0; i < y; i++) { int a = 0; for(int j = 0; j < x; j++) { string c; cin >> c; if(c[0] == 'R') { a |= 0 << (2*j); } else if(c[0] == 'G') { a |= 1 << (2*j); } else { a |= 2 << (2*j); } } v.push_back(a); } queue<vector<int> > q; q.push(v); int ans = -1; for(int t = 0;; t++) { queue<vector<int> > nex; while(!q.empty()) { vector<int> vn[2]; vn[0] = vn[1] = q.front(); q.pop(); bool flg = true; for(int i = 0; i < y; i++) { for(int j = 0; j < x; j++) { if(((vn[0][i] >> (2*j)) & 3) != (vn[0][0] & 3)) flg = false; } } if(flg) { ans = t; goto exit; } int ch = vn[0][0] & 3; vn[0][0] &= ~3; vn[0][0] |= (ch+1)%3; solve(0,0,vn[0],ch,(ch+1)%3); vn[1][0] &= ~3; vn[1][0] |= (ch+2)%3; solve(0,0,vn[1],ch,(ch+2)%3); nex.push(vn[0]); nex.push(vn[1]); } q = nex; } exit:; cout << ans << endl; } } ```
#include<iostream> #include<cstring> using namespace std; #define INF (1<<29) #define rep(i,n) for(int i=0;i<(n);++i) int w,h; int ans; int dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; bool visit[10][10]; inline bool check(int y,int x){ return 0<=y&&y<h&&0<=x&&x<w; } int neighbor(int y,int x,char color,char panel[10][10]){ int res=0; if(!check(y,x) || visit[y][x])return 0; if(panel[y][x]!=color){ if(panel[y][x]=='R')return 1; if(panel[y][x]=='G')return 2; if(panel[y][x]=='B')return 4; } visit[y][x] = true; rep(i,4){ res |= neighbor(y+dy[i],x+dx[i],color,panel); } return res; } void change(int y,int x,char from,char to,char panel[10][10]){ if(!check(y,x) || from!=panel[y][x])return; panel[y][x] = to; rep(i,4){ change(y+dy[i],x+dx[i],from,to,panel); } } void search(int depth,char panel[10][10]){ if(ans<=depth)return; memset(visit,false,sizeof(visit)); int diff = neighbor(0,0,panel[0][0],panel); if(diff==0){ ans = depth; return; } char buf[10][10]; rep(i,3){ if((diff&1<<i)==0)continue; memcpy(buf,panel,sizeof(buf)); change(0,0,buf[0][0], (i==0?'R':(i==1?'G':'B')),buf); search(depth+1,buf); if(ans <= depth+1)return; } } int main(){ char panel[10][10]; while(cin>>w>>h,w|h){ rep(i,h)rep(j,w)cin>>panel[i][j]; ans = INF; search(0,panel); cout<<ans<<endl; } }
### Prompt Please provide a Cpp coded solution to the problem described below: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include<iostream> #include<cstring> using namespace std; #define INF (1<<29) #define rep(i,n) for(int i=0;i<(n);++i) int w,h; int ans; int dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; bool visit[10][10]; inline bool check(int y,int x){ return 0<=y&&y<h&&0<=x&&x<w; } int neighbor(int y,int x,char color,char panel[10][10]){ int res=0; if(!check(y,x) || visit[y][x])return 0; if(panel[y][x]!=color){ if(panel[y][x]=='R')return 1; if(panel[y][x]=='G')return 2; if(panel[y][x]=='B')return 4; } visit[y][x] = true; rep(i,4){ res |= neighbor(y+dy[i],x+dx[i],color,panel); } return res; } void change(int y,int x,char from,char to,char panel[10][10]){ if(!check(y,x) || from!=panel[y][x])return; panel[y][x] = to; rep(i,4){ change(y+dy[i],x+dx[i],from,to,panel); } } void search(int depth,char panel[10][10]){ if(ans<=depth)return; memset(visit,false,sizeof(visit)); int diff = neighbor(0,0,panel[0][0],panel); if(diff==0){ ans = depth; return; } char buf[10][10]; rep(i,3){ if((diff&1<<i)==0)continue; memcpy(buf,panel,sizeof(buf)); change(0,0,buf[0][0], (i==0?'R':(i==1?'G':'B')),buf); search(depth+1,buf); if(ans <= depth+1)return; } } int main(){ char panel[10][10]; while(cin>>w>>h,w|h){ rep(i,h)rep(j,w)cin>>panel[i][j]; ans = INF; search(0,panel); cout<<ans<<endl; } } ```
#include <iostream> #include <iomanip> #include <sstream> #include <cstdio> #include <string> #include <vector> #include <algorithm> #include <complex> #include <cstring> #include <cstdlib> #include <cmath> #include <cassert> #include <climits> #include <queue> #include <set> #include <map> #include <valarray> #include <bitset> #include <stack> using namespace std; #define REP(i,n) for(int i=0;i<(int)n;++i) #define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i) #define ALL(c) (c).begin(), (c).end() typedef long long ll; typedef pair<int,int> pii; const int INF = 1<<29; const double PI = acos(-1); const double EPS = 1e-8; int h,w; // ll encode(int a[10][10]) { // ll res = 0; // REP(i,h)REP(j,w) res = res*3+a[i][j]; // return res; // } // void decode(int a[10][10], ll e) { // for (int i=h-1; i>=0; --i) { // for (int j=w-1; j>=0; --j) { // a[i][j] = e%3; // e /= 3; // } // } // } typedef vector<int> vec; typedef vector<vec> mat; // int ba[10][10]; // int now[10][10]; // int nxt[10][10]; bool visited[10][10]; int dy[4] = {-1,0,1,0}; int dx[4] = {0,1,0,-1}; int ans; void dfs(mat now, int d) { // cout << d << endl; // REP(i,h) { // REP(j,w) cout << now[i][j]; // cout << endl; // } int col = now[0][0]; queue<pii> Q; Q.push(pii(0,0)); REP(i,h)REP(j,w)visited[i][j] = 0; visited[0][0] = 1; vector<pii> ps; bool ncol[3] = {}; while(!Q.empty()) { pii p = Q.front(); Q.pop(); ps.push_back(p); REP(i,4) { int y = p.first + dy[i]; int x = p.second + dx[i]; if (y<0||y>=h || x<0||x>=w)continue; if (now[y][x] != col) { ncol[now[y][x]] = 1; continue; } if (!visited[y][x]) { visited[y][x] = 1; Q.push(pii(y,x)); } } } if (ps.size() == h*w) { ans = min(ans,d); return; } if (d == ans-1) return; REP(k,3) { if (ncol[k] == 0) continue; FOR(it, ps) { now[it->first][it->second] = k; } dfs(now, d+1); } } int main() { while(cin>>w>>h,h||w) { mat ba(h,vec(w)); REP(i,h) { REP(j,w) { char c; cin >> c; if (c == 'R') ba[i][j] = 0; else if (c == 'G') ba[i][j] = 1; else ba[i][j] = 2; } } ans = INF; dfs(ba, 0); cout << ans << endl; } }
### Prompt Please provide a cpp coded solution to the problem described below: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <iostream> #include <iomanip> #include <sstream> #include <cstdio> #include <string> #include <vector> #include <algorithm> #include <complex> #include <cstring> #include <cstdlib> #include <cmath> #include <cassert> #include <climits> #include <queue> #include <set> #include <map> #include <valarray> #include <bitset> #include <stack> using namespace std; #define REP(i,n) for(int i=0;i<(int)n;++i) #define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i) #define ALL(c) (c).begin(), (c).end() typedef long long ll; typedef pair<int,int> pii; const int INF = 1<<29; const double PI = acos(-1); const double EPS = 1e-8; int h,w; // ll encode(int a[10][10]) { // ll res = 0; // REP(i,h)REP(j,w) res = res*3+a[i][j]; // return res; // } // void decode(int a[10][10], ll e) { // for (int i=h-1; i>=0; --i) { // for (int j=w-1; j>=0; --j) { // a[i][j] = e%3; // e /= 3; // } // } // } typedef vector<int> vec; typedef vector<vec> mat; // int ba[10][10]; // int now[10][10]; // int nxt[10][10]; bool visited[10][10]; int dy[4] = {-1,0,1,0}; int dx[4] = {0,1,0,-1}; int ans; void dfs(mat now, int d) { // cout << d << endl; // REP(i,h) { // REP(j,w) cout << now[i][j]; // cout << endl; // } int col = now[0][0]; queue<pii> Q; Q.push(pii(0,0)); REP(i,h)REP(j,w)visited[i][j] = 0; visited[0][0] = 1; vector<pii> ps; bool ncol[3] = {}; while(!Q.empty()) { pii p = Q.front(); Q.pop(); ps.push_back(p); REP(i,4) { int y = p.first + dy[i]; int x = p.second + dx[i]; if (y<0||y>=h || x<0||x>=w)continue; if (now[y][x] != col) { ncol[now[y][x]] = 1; continue; } if (!visited[y][x]) { visited[y][x] = 1; Q.push(pii(y,x)); } } } if (ps.size() == h*w) { ans = min(ans,d); return; } if (d == ans-1) return; REP(k,3) { if (ncol[k] == 0) continue; FOR(it, ps) { now[it->first][it->second] = k; } dfs(now, d+1); } } int main() { while(cin>>w>>h,h||w) { mat ba(h,vec(w)); REP(i,h) { REP(j,w) { char c; cin >> c; if (c == 'R') ba[i][j] = 0; else if (c == 'G') ba[i][j] = 1; else ba[i][j] = 2; } } ans = INF; dfs(ba, 0); cout << ans << endl; } } ```
#include <iostream> #include <stdio.h> #include <sstream> #include <string> #include <vector> #include <map> #include <queue> #include <algorithm> #include <set> #include <math.h> #include <utility> #include <stack> #include <string.h> #include <complex> using namespace std; const int INF = 1<<29; const double EPS = 1e-8; //typedef vector<int> vec; typedef pair<int,int> P; struct edge{int to,cost;}; typedef vector<char> vec; typedef vector<vec> mat; const int dx[] = {0,0,1,-1}; const int dy[] = {1,-1,0,0}; int X, Y; struct State{ int d,pn; char c; mat f; bool operator<(const State& right) const{ return d == right.d ? pn < right.pn : d > right.d; } }; int paint(int y, int x, char c, char nc, mat& f){ f[y][x] = nc; int ret = 1; for(int i=0;i<4;i++){ int nx = x+dx[i], ny = y+dy[i]; if(nx<0||X<=nx||ny<0||Y<=ny||f[ny][nx]!=c) continue; ret += paint(ny, nx, c, nc, f); } return ret; } const char color[] = {'R','G','B'}; int dfs(int pn, mat& f){ int turn = 100; for(int i=0;i<3;i++){ if(f[0][0]==color[i])continue; mat nf = f; int npn = paint(0,0,f[0][0],color[i],nf); if(npn==X*Y) return 0; if(npn<=pn) continue; else turn = min(turn, 1+dfs(npn, nf)); } return turn; } int main(){ while(cin >> X >> Y, X){ mat field(Y, vec(X)); for(int i=0;i<Y;i++){ for(int j=0;j<X;j++){ cin >> field[i][j]; } } priority_queue<State> que; //int d[100][100]; //for(int i=0;i<100;i++)fill(d[i],d[i]+100,INF); int d[101];fill(d,d+101,INF); d[0] = 0; que.push((State){0,0,field[0][0],field}); bool ans_found = false; while(que.size()){ State p = que.top(); que.pop(); if(d[p.pn]<p.d) continue; for(int i=0;i<3;i++){ if(p.c==color[i])continue; mat nf = p.f; int npn = paint(0,0,p.f[0][0],color[i],nf); if(npn == X*Y){ ans_found = true; break; } if(npn <= p.pn) continue; if(d[npn]<p.d+1) continue; d[npn] = p.d+1; que.push((State){p.d+1,npn,color[i],nf}); } if(ans_found){ cout << p.d << endl; break; } } } return 0; }
### Prompt Please formulate a CPP solution to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <iostream> #include <stdio.h> #include <sstream> #include <string> #include <vector> #include <map> #include <queue> #include <algorithm> #include <set> #include <math.h> #include <utility> #include <stack> #include <string.h> #include <complex> using namespace std; const int INF = 1<<29; const double EPS = 1e-8; //typedef vector<int> vec; typedef pair<int,int> P; struct edge{int to,cost;}; typedef vector<char> vec; typedef vector<vec> mat; const int dx[] = {0,0,1,-1}; const int dy[] = {1,-1,0,0}; int X, Y; struct State{ int d,pn; char c; mat f; bool operator<(const State& right) const{ return d == right.d ? pn < right.pn : d > right.d; } }; int paint(int y, int x, char c, char nc, mat& f){ f[y][x] = nc; int ret = 1; for(int i=0;i<4;i++){ int nx = x+dx[i], ny = y+dy[i]; if(nx<0||X<=nx||ny<0||Y<=ny||f[ny][nx]!=c) continue; ret += paint(ny, nx, c, nc, f); } return ret; } const char color[] = {'R','G','B'}; int dfs(int pn, mat& f){ int turn = 100; for(int i=0;i<3;i++){ if(f[0][0]==color[i])continue; mat nf = f; int npn = paint(0,0,f[0][0],color[i],nf); if(npn==X*Y) return 0; if(npn<=pn) continue; else turn = min(turn, 1+dfs(npn, nf)); } return turn; } int main(){ while(cin >> X >> Y, X){ mat field(Y, vec(X)); for(int i=0;i<Y;i++){ for(int j=0;j<X;j++){ cin >> field[i][j]; } } priority_queue<State> que; //int d[100][100]; //for(int i=0;i<100;i++)fill(d[i],d[i]+100,INF); int d[101];fill(d,d+101,INF); d[0] = 0; que.push((State){0,0,field[0][0],field}); bool ans_found = false; while(que.size()){ State p = que.top(); que.pop(); if(d[p.pn]<p.d) continue; for(int i=0;i<3;i++){ if(p.c==color[i])continue; mat nf = p.f; int npn = paint(0,0,p.f[0][0],color[i],nf); if(npn == X*Y){ ans_found = true; break; } if(npn <= p.pn) continue; if(d[npn]<p.d+1) continue; d[npn] = p.d+1; que.push((State){p.d+1,npn,color[i],nf}); } if(ans_found){ cout << p.d << endl; break; } } } return 0; } ```
#include<iostream> #include<queue> using namespace std; int x,y; struct grid{ char g[11][11]; int count; }; void DFS(grid& gr,char firstcell,char changecolor,int posx,int posy){ if(posx < 0 || posx >= x || posy < 0 || posy >= y || gr.g[posx][posy] != firstcell){ return; } gr.g[posx][posy] = changecolor; DFS(gr,firstcell,changecolor,posx+1,posy); DFS(gr,firstcell,changecolor,posx-1,posy); DFS(gr,firstcell,changecolor,posx,posy+1); DFS(gr,firstcell,changecolor,posx,posy-1); } int main(){ while(1){ cin >> x >> y; if(x==0 && y==0){ break; } grid start; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ cin >> start.g[j][i]; } } char firstcell = start.g[0][0]; bool flag = true; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ if(start.g[j][i]!=firstcell){ flag = false; break; } } if(!flag){ break; } } if(flag){ cout << 0 << endl; continue; } start.count = 0; queue<grid> q; q.push(start); while(1){ grid BeforeGrid = q.front(); q.pop(); BeforeGrid.count++; grid tmp = BeforeGrid; firstcell = tmp.g[0][0]; if(firstcell != 'R'){ DFS(tmp,firstcell,'R',0,0); flag = true; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ if(tmp.g[j][i]!='R'){ flag = false; break; } } if(!flag){ break; } } if(flag){ cout << tmp.count << endl; break; } q.push(tmp); tmp = BeforeGrid; } if(firstcell != 'G'){ DFS(tmp,firstcell,'G',0,0); flag = true; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ if(tmp.g[j][i]!='G'){ flag = false; break; } } if(!flag){ break; } } if(flag){ cout << tmp.count << endl; break; } q.push(tmp); tmp = BeforeGrid; } if(firstcell != 'B'){ DFS(tmp,firstcell,'B',0,0); flag = true; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ if(tmp.g[j][i]!='B'){ flag = false; break; } } if(!flag){ break; } } if(flag){ cout << tmp.count << endl; break; } q.push(tmp); tmp = BeforeGrid; } } } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include<iostream> #include<queue> using namespace std; int x,y; struct grid{ char g[11][11]; int count; }; void DFS(grid& gr,char firstcell,char changecolor,int posx,int posy){ if(posx < 0 || posx >= x || posy < 0 || posy >= y || gr.g[posx][posy] != firstcell){ return; } gr.g[posx][posy] = changecolor; DFS(gr,firstcell,changecolor,posx+1,posy); DFS(gr,firstcell,changecolor,posx-1,posy); DFS(gr,firstcell,changecolor,posx,posy+1); DFS(gr,firstcell,changecolor,posx,posy-1); } int main(){ while(1){ cin >> x >> y; if(x==0 && y==0){ break; } grid start; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ cin >> start.g[j][i]; } } char firstcell = start.g[0][0]; bool flag = true; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ if(start.g[j][i]!=firstcell){ flag = false; break; } } if(!flag){ break; } } if(flag){ cout << 0 << endl; continue; } start.count = 0; queue<grid> q; q.push(start); while(1){ grid BeforeGrid = q.front(); q.pop(); BeforeGrid.count++; grid tmp = BeforeGrid; firstcell = tmp.g[0][0]; if(firstcell != 'R'){ DFS(tmp,firstcell,'R',0,0); flag = true; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ if(tmp.g[j][i]!='R'){ flag = false; break; } } if(!flag){ break; } } if(flag){ cout << tmp.count << endl; break; } q.push(tmp); tmp = BeforeGrid; } if(firstcell != 'G'){ DFS(tmp,firstcell,'G',0,0); flag = true; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ if(tmp.g[j][i]!='G'){ flag = false; break; } } if(!flag){ break; } } if(flag){ cout << tmp.count << endl; break; } q.push(tmp); tmp = BeforeGrid; } if(firstcell != 'B'){ DFS(tmp,firstcell,'B',0,0); flag = true; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ if(tmp.g[j][i]!='B'){ flag = false; break; } } if(!flag){ break; } } if(flag){ cout << tmp.count << endl; break; } q.push(tmp); tmp = BeforeGrid; } } } return 0; } ```
#include <iostream> #include <string> #include <vector> #include <cstring> #include <climits> #include <algorithm> #include <map> using namespace std; const int dx[4] = {1, -1, 0, 0}; const int dy[4] = {0, 0, 1, -1}; int X, Y; int bd[10][10]; inline bool valid(int x, int y) { return 0 <= x && x < X && 0 <= y && y < Y; } bool ok() { for (int i=0; i<Y; ++i) { for (int j=0; j<X; ++j) { if (bd[0][0] != bd[i][j]) return false; } } return true; } bool paint(int color, int x, int y) { bool ret = false; int orig = bd[y][x]; bd[y][x] = color; for (int i=0; i<4; ++i) { if (valid(x+dx[i], y+dy[i])) { if (bd[y+dy[i]][x+dx[i]] == orig) { ret |= paint(color, x+dx[i], y+dy[i]); } else { ret = true; } } } return ret; } int dfs(int cnt, int &mn) { if (ok()) { mn = min(mn, cnt); return cnt; } if (20 < cnt || mn <= cnt) return INT_MAX; int ret = INT_MAX; int tmp[10][10]; memcpy(tmp, bd, sizeof tmp); if (paint((bd[0][0] + 1) % 3, 0, 0)) ret = min(ret, dfs(cnt + 1, mn)); memcpy(bd, tmp, sizeof bd); if (paint((bd[0][0] + 2) % 3, 0, 0)) ret = min(ret, dfs(cnt + 1, mn)); memcpy(bd, tmp, sizeof bd); return ret; } int main() { while (cin >> X >> Y, X) { char c; for (int i=0; i<Y; ++i) { for (int j=0; j<X; ++j) { cin >> c; bd[i][j] = (c == 'R' ? 0 : (c == 'G' ? 1 : 2)); } } int mn = INT_MAX; cout << dfs(0, mn) << endl; } return 0; }
### Prompt Develop a solution in Cpp to the problem described below: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <iostream> #include <string> #include <vector> #include <cstring> #include <climits> #include <algorithm> #include <map> using namespace std; const int dx[4] = {1, -1, 0, 0}; const int dy[4] = {0, 0, 1, -1}; int X, Y; int bd[10][10]; inline bool valid(int x, int y) { return 0 <= x && x < X && 0 <= y && y < Y; } bool ok() { for (int i=0; i<Y; ++i) { for (int j=0; j<X; ++j) { if (bd[0][0] != bd[i][j]) return false; } } return true; } bool paint(int color, int x, int y) { bool ret = false; int orig = bd[y][x]; bd[y][x] = color; for (int i=0; i<4; ++i) { if (valid(x+dx[i], y+dy[i])) { if (bd[y+dy[i]][x+dx[i]] == orig) { ret |= paint(color, x+dx[i], y+dy[i]); } else { ret = true; } } } return ret; } int dfs(int cnt, int &mn) { if (ok()) { mn = min(mn, cnt); return cnt; } if (20 < cnt || mn <= cnt) return INT_MAX; int ret = INT_MAX; int tmp[10][10]; memcpy(tmp, bd, sizeof tmp); if (paint((bd[0][0] + 1) % 3, 0, 0)) ret = min(ret, dfs(cnt + 1, mn)); memcpy(bd, tmp, sizeof bd); if (paint((bd[0][0] + 2) % 3, 0, 0)) ret = min(ret, dfs(cnt + 1, mn)); memcpy(bd, tmp, sizeof bd); return ret; } int main() { while (cin >> X >> Y, X) { char c; for (int i=0; i<Y; ++i) { for (int j=0; j<X; ++j) { cin >> c; bd[i][j] = (c == 'R' ? 0 : (c == 'G' ? 1 : 2)); } } int mn = INT_MAX; cout << dfs(0, mn) << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int w, h; int dx[] = {0, 0, -1, 1}; int dy[] = {-1, 1, 0, 0}; char rgb[] = "RGB"; struct Grid { int cnt; string stat; }; bool check(string s) { int p = 1; while(s[p] == s[p - 1] && p < s.size()) ++p; return p == s.size(); } void rep(string &stat, char from, char to, int x, int y) { stat[y * w + x] = to; for(int i = 0; i < 4; ++i) { int nx = x + dx[i]; int ny = y + dy[i]; if(nx >= 0 && nx < w && ny >= 0 && ny < h && stat[ny * w + nx] == from) { rep(stat, from, to, nx, ny); } } return; } int main() { char graph[10][10]; while(cin >> w >> h, w | h) { string stat = ""; for(int i = 0; i < h; ++i) { for(int j = 0; j < w; ++j) { cin >> graph[i][j]; stat += graph[i][j]; } } //queue<int> map<string, bool> mp; queue<Grid> q; q.push((Grid){0, stat}); mp[stat] = true; while(!q.empty()) { Grid now = q.front(); q.pop(); //cout << now.stat << endl; if(check(now.stat)) { cout << now.cnt << endl; break; } for(int i = 0; i < 3; ++i) { Grid next = (Grid){now.cnt + 1, now.stat}; if(next.stat[0] != rgb[i]) { rep(next.stat, next.stat[0], rgb[i], 0, 0); if(mp.count(next.stat) == 0) { mp[next.stat] = true; q.push(next); } } } } } }
### Prompt Your challenge is to write a cpp solution to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int w, h; int dx[] = {0, 0, -1, 1}; int dy[] = {-1, 1, 0, 0}; char rgb[] = "RGB"; struct Grid { int cnt; string stat; }; bool check(string s) { int p = 1; while(s[p] == s[p - 1] && p < s.size()) ++p; return p == s.size(); } void rep(string &stat, char from, char to, int x, int y) { stat[y * w + x] = to; for(int i = 0; i < 4; ++i) { int nx = x + dx[i]; int ny = y + dy[i]; if(nx >= 0 && nx < w && ny >= 0 && ny < h && stat[ny * w + nx] == from) { rep(stat, from, to, nx, ny); } } return; } int main() { char graph[10][10]; while(cin >> w >> h, w | h) { string stat = ""; for(int i = 0; i < h; ++i) { for(int j = 0; j < w; ++j) { cin >> graph[i][j]; stat += graph[i][j]; } } //queue<int> map<string, bool> mp; queue<Grid> q; q.push((Grid){0, stat}); mp[stat] = true; while(!q.empty()) { Grid now = q.front(); q.pop(); //cout << now.stat << endl; if(check(now.stat)) { cout << now.cnt << endl; break; } for(int i = 0; i < 3; ++i) { Grid next = (Grid){now.cnt + 1, now.stat}; if(next.stat[0] != rgb[i]) { rep(next.stat, next.stat[0], rgb[i], 0, 0); if(mp.count(next.stat) == 0) { mp[next.stat] = true; q.push(next); } } } } } } ```
#include<iostream> #include<queue> #include<vector> #define INF 1000 using namespace std; typedef pair<int,int> P; int H,W; int mindep; //最小手数 char MAP[12][12]; //可変マップデータ( 外側番兵 ) char has[4] = "RGB"; //領域の面積を戻り値とする int BFS( char color ){ queue<P> que; P now; char map11 = MAP[1][1]; int i; int dx[4] = {0,1,0,-1}; int dy[4] = {1,0,-1,0}; int ret = 0; que.push( P(1,1) ); while( !que.empty() ){ now = que.front(); que.pop(); if( MAP[ now.first ][ now.second ] != map11 ) continue; ret++; MAP[ now.first ][ now.second ] = color; for( i = 0;i < 4;i++ ) que.push( P( now.first + dy[i], now.second + dx[i] ) ); } return ret; } //depth=操作回数,maxcount1=最多の色ブロックの個数(操作前) int draw( int depth,int maxcount1 ) { char color = MAP[1][1]; //1行1列の色(文字) char map[12][12] = {0}; //今のマップデータ //int cnt[3] = {0}; //R,G,Bブロックの数 int maxcount2; //今の最多の色ブロックの個数(操作前) int i,j; if( mindep <= depth ) return INF; for( i = 1;i < H+1;i++ ){ for( j = 1;j < W+1;j++ ){ map[i][j] = MAP[i][j]; /*if( MAP[i][j] == 'R' ) cnt[0]++; if( MAP[i][j] == 'B' ) cnt[1]++; if( MAP[i][j] == 'G' ) cnt[2]++;*/ } } //cout << cnt[0] << " " << cnt[1] << " " << cnt[2] << endl; int ret[3] = {1000,1000,1000}; //戻り値 for( i = 0;i < 3;i++ ){ if( has[i] == color ) continue; //MAP[1][1]と同じ色の1つの領域をhas[i]色にする. maxcount2 = BFS( has[i] ); if( maxcount2 == H*W ) return depth; if( maxcount1 >= maxcount2 ) { continue; } ret[i] = draw( depth+1,maxcount2 ); //MAPの内容をもとに戻す for( j = 1;j < H+1;j++ ){ for( int k = 1;k < W+1;k++ ){ MAP[j][k] = map[j][k]; } } } mindep = min( mindep, min( ret[0], min(ret[1],ret[2]) ) ); return min( ret[0], min(ret[1],ret[2]) ); } int main() { int i,j,k; int cnt[3] = {0}; //R,B,Gブロックの数 int ans[1000] = {0}; for( i = 0; cin >> W >> H; i++ ){ if( W == 0 && H == 0 ) break; mindep = INF; for( j = 0;j < H+2;j++ ){ for( k = 0;k < W+2;k++ ){ MAP[j][k] = 0; } } for( j = 1;j < H+1;j++ ){ for( k = 1;k < W+1;k++ ){ cin >> MAP[j][k]; if( MAP[j][k] == '\n' || MAP[j][k] == '\r' ){ k--; continue; } } } ans[i] = draw( 0,0 ); cout << ans[i] << endl; } /*for( j = 0;j < i;j++ ){ cout << ans[j] << endl; }*/ return 0; }
### Prompt Develop a solution in Cpp to the problem described below: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include<iostream> #include<queue> #include<vector> #define INF 1000 using namespace std; typedef pair<int,int> P; int H,W; int mindep; //最小手数 char MAP[12][12]; //可変マップデータ( 外側番兵 ) char has[4] = "RGB"; //領域の面積を戻り値とする int BFS( char color ){ queue<P> que; P now; char map11 = MAP[1][1]; int i; int dx[4] = {0,1,0,-1}; int dy[4] = {1,0,-1,0}; int ret = 0; que.push( P(1,1) ); while( !que.empty() ){ now = que.front(); que.pop(); if( MAP[ now.first ][ now.second ] != map11 ) continue; ret++; MAP[ now.first ][ now.second ] = color; for( i = 0;i < 4;i++ ) que.push( P( now.first + dy[i], now.second + dx[i] ) ); } return ret; } //depth=操作回数,maxcount1=最多の色ブロックの個数(操作前) int draw( int depth,int maxcount1 ) { char color = MAP[1][1]; //1行1列の色(文字) char map[12][12] = {0}; //今のマップデータ //int cnt[3] = {0}; //R,G,Bブロックの数 int maxcount2; //今の最多の色ブロックの個数(操作前) int i,j; if( mindep <= depth ) return INF; for( i = 1;i < H+1;i++ ){ for( j = 1;j < W+1;j++ ){ map[i][j] = MAP[i][j]; /*if( MAP[i][j] == 'R' ) cnt[0]++; if( MAP[i][j] == 'B' ) cnt[1]++; if( MAP[i][j] == 'G' ) cnt[2]++;*/ } } //cout << cnt[0] << " " << cnt[1] << " " << cnt[2] << endl; int ret[3] = {1000,1000,1000}; //戻り値 for( i = 0;i < 3;i++ ){ if( has[i] == color ) continue; //MAP[1][1]と同じ色の1つの領域をhas[i]色にする. maxcount2 = BFS( has[i] ); if( maxcount2 == H*W ) return depth; if( maxcount1 >= maxcount2 ) { continue; } ret[i] = draw( depth+1,maxcount2 ); //MAPの内容をもとに戻す for( j = 1;j < H+1;j++ ){ for( int k = 1;k < W+1;k++ ){ MAP[j][k] = map[j][k]; } } } mindep = min( mindep, min( ret[0], min(ret[1],ret[2]) ) ); return min( ret[0], min(ret[1],ret[2]) ); } int main() { int i,j,k; int cnt[3] = {0}; //R,B,Gブロックの数 int ans[1000] = {0}; for( i = 0; cin >> W >> H; i++ ){ if( W == 0 && H == 0 ) break; mindep = INF; for( j = 0;j < H+2;j++ ){ for( k = 0;k < W+2;k++ ){ MAP[j][k] = 0; } } for( j = 1;j < H+1;j++ ){ for( k = 1;k < W+1;k++ ){ cin >> MAP[j][k]; if( MAP[j][k] == '\n' || MAP[j][k] == '\r' ){ k--; continue; } } } ans[i] = draw( 0,0 ); cout << ans[i] << endl; } /*for( j = 0;j < i;j++ ){ cout << ans[j] << endl; }*/ return 0; } ```
#include<iostream> #include<algorithm> #include<map> #include<queue> #include<cassert> using namespace std; #define MAX 20 class State{ public: char T[MAX][MAX], w, h, cost; State(){} State(int w, int h):w(w), h(h){ cost = 0;} bool operator < ( const State &s ) const{ for ( int i = 0; i < h; i++ ){ for ( int j = 0; j < w; j++ ){ if ( T[i][j] == s.T[i][j] ) continue; return T[i][j] < s.T[i][j]; } } return false; } bool solved(){ char t = T[0][0]; for ( int i = 0; i < h; i++ ){ for ( int j = 0; j < w; j++ ){ if ( T[i][j] != t ) return false; } } return true; } }; void dfs( State &u, int y, int x, char s, char t ){ u.T[y][x] = t; static const int dy[4] = {0, -1, 0, 1}; static const int dx[4] = {1, 0, -1, 0}; int ny, nx; for ( int r = 0; r < 4; r++ ){ ny = y + dy[r]; nx = x + dx[r]; if ( ny < 0 || nx < 0 || u.h <= ny || u.w <= nx){ continue; } if ( u.T[ny][nx] == s ) dfs(u, ny, nx, s, t); } } int bfs(State init){ queue<State> Q; map<State, bool> V; init.cost = 0; V[init] = true; Q.push(init); string tb = "RGB"; State u, v; while(!Q.empty() ){ u = Q.front(); Q.pop(); if ( u.solved() ) return u.cost; for ( int r = 0; r < 3; r++ ){ v = u; if ( v.T[0][0] == tb[r] ) continue; dfs(v, 0, 0, v.T[0][0], tb[r] ); if ( !V[v] ){ v.cost = u.cost + 1; V[v] = true; Q.push(v); } } } return -1; } int main(){ int w, h; while(1){ cin >> w >> h; if ( w== 0 && h == 0 ) break; assert( 2 <= w && w <= 10 ); assert( 2 <= h && h <= 10 ); State init = State(w, h); for ( int i = 0; i < h; i++ ){ for ( int j = 0; j < w; j++ ){ cin >> init.T[i][j]; } } cout << bfs(init) << endl; } }
### Prompt Please create a solution in cpp to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include<iostream> #include<algorithm> #include<map> #include<queue> #include<cassert> using namespace std; #define MAX 20 class State{ public: char T[MAX][MAX], w, h, cost; State(){} State(int w, int h):w(w), h(h){ cost = 0;} bool operator < ( const State &s ) const{ for ( int i = 0; i < h; i++ ){ for ( int j = 0; j < w; j++ ){ if ( T[i][j] == s.T[i][j] ) continue; return T[i][j] < s.T[i][j]; } } return false; } bool solved(){ char t = T[0][0]; for ( int i = 0; i < h; i++ ){ for ( int j = 0; j < w; j++ ){ if ( T[i][j] != t ) return false; } } return true; } }; void dfs( State &u, int y, int x, char s, char t ){ u.T[y][x] = t; static const int dy[4] = {0, -1, 0, 1}; static const int dx[4] = {1, 0, -1, 0}; int ny, nx; for ( int r = 0; r < 4; r++ ){ ny = y + dy[r]; nx = x + dx[r]; if ( ny < 0 || nx < 0 || u.h <= ny || u.w <= nx){ continue; } if ( u.T[ny][nx] == s ) dfs(u, ny, nx, s, t); } } int bfs(State init){ queue<State> Q; map<State, bool> V; init.cost = 0; V[init] = true; Q.push(init); string tb = "RGB"; State u, v; while(!Q.empty() ){ u = Q.front(); Q.pop(); if ( u.solved() ) return u.cost; for ( int r = 0; r < 3; r++ ){ v = u; if ( v.T[0][0] == tb[r] ) continue; dfs(v, 0, 0, v.T[0][0], tb[r] ); if ( !V[v] ){ v.cost = u.cost + 1; V[v] = true; Q.push(v); } } } return -1; } int main(){ int w, h; while(1){ cin >> w >> h; if ( w== 0 && h == 0 ) break; assert( 2 <= w && w <= 10 ); assert( 2 <= h && h <= 10 ); State init = State(w, h); for ( int i = 0; i < h; i++ ){ for ( int j = 0; j < w; j++ ){ cin >> init.T[i][j]; } } cout << bfs(init) << endl; } } ```
#include <bits/stdc++.h> using namespace std; using ll = long long int; using P = pair<int, int>; using P3 = pair<int,P>; using PP = pair<P, P>; constexpr int INF = 1<<29; constexpr ll MOD = ll(1e9)+7; constexpr int di[] = {0,1,0,-1}; constexpr int dj[] = {1,0,-1,0}; int h, w, ans; void solve(vector<vector<int> > &a, vector<queue<P> > que, int cnt = -1){ bool update = false; vector<P3> tmp; auto q = que; for(int k=0;k<3;k++){ if(que[k].empty()) continue; update = true; tmp.clear(); while(!que[k].empty()){ P p = que[k].front(); que[k].pop(); int i = p.first, j = p.second; for(int x=0;x<4;x++){ int ni = i+di[x], nj = j+dj[x]; if(ni < 0 || ni >= h) continue; if(nj < 0 || nj >= w) continue; if(a[ni][nj] != 4){ tmp.push_back(P3(a[ni][nj],P(ni,nj))); que[a[ni][nj]].push(P(ni,nj)); a[ni][nj] = 4; } } } if(ans >= cnt) solve(a, que, cnt+1); for(auto pp : tmp){ int i = pp.second.first, j = pp.second.second; a[i][j] = pp.first; } que = q; } if(!update) ans = min(ans, cnt); } int main(){ vector<vector<int> > a; while(cin >> w >> h, w){ a.clear(); a.resize(h, vector<int>(w)); for(int i=0;i<h;i++){ for(int j=0;j<w;j++){ char c; cin >> c; if(c == 'R') a[i][j] = 0; if(c == 'G') a[i][j] = 1; if(c == 'B') a[i][j] = 2; } } ans = INF; vector<queue<P> > q(3); q[a[0][0]].push(P(0,0)); a[0][0] = 4; solve(a, q); cout << ans << endl; } return 0; }
### Prompt In CPP, your task is to solve the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long int; using P = pair<int, int>; using P3 = pair<int,P>; using PP = pair<P, P>; constexpr int INF = 1<<29; constexpr ll MOD = ll(1e9)+7; constexpr int di[] = {0,1,0,-1}; constexpr int dj[] = {1,0,-1,0}; int h, w, ans; void solve(vector<vector<int> > &a, vector<queue<P> > que, int cnt = -1){ bool update = false; vector<P3> tmp; auto q = que; for(int k=0;k<3;k++){ if(que[k].empty()) continue; update = true; tmp.clear(); while(!que[k].empty()){ P p = que[k].front(); que[k].pop(); int i = p.first, j = p.second; for(int x=0;x<4;x++){ int ni = i+di[x], nj = j+dj[x]; if(ni < 0 || ni >= h) continue; if(nj < 0 || nj >= w) continue; if(a[ni][nj] != 4){ tmp.push_back(P3(a[ni][nj],P(ni,nj))); que[a[ni][nj]].push(P(ni,nj)); a[ni][nj] = 4; } } } if(ans >= cnt) solve(a, que, cnt+1); for(auto pp : tmp){ int i = pp.second.first, j = pp.second.second; a[i][j] = pp.first; } que = q; } if(!update) ans = min(ans, cnt); } int main(){ vector<vector<int> > a; while(cin >> w >> h, w){ a.clear(); a.resize(h, vector<int>(w)); for(int i=0;i<h;i++){ for(int j=0;j<w;j++){ char c; cin >> c; if(c == 'R') a[i][j] = 0; if(c == 'G') a[i][j] = 1; if(c == 'B') a[i][j] = 2; } } ans = INF; vector<queue<P> > q(3); q[a[0][0]].push(P(0,0)); a[0][0] = 4; solve(a, q); cout << ans << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; #define rep(i,x,y) for(int i=(x);i<(y);++i) #define debug(x) #x << "=" << (x) #ifdef DEBUG #define _GLIBCXX_DEBUG #define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl #else #define dump(x) #endif typedef long long int ll; typedef pair<int,int> pii; //template<typename T> using vec=std::vector<T>; const int inf=1<<30; const long long int infll=1LL<<58; const double eps=1e-9; const int dx[]={1,0,-1,0},dy[]={0,1,0,-1}; template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (const auto &v : vec) { os << v << ","; } os << "]"; return os; } int x,y,ans,min_res=0; inline vector<pii> fill_cells(vector<vector<char>>& cells,char color){ vector<pii> res; queue<pii> que; que.push(make_pair(0,0)); char prev_color=cells[0][0]; cells[0][0]=color; while(!que.empty()){ pii p=que.front(); que.pop(); res.push_back(p); for(int i=0; i<4; ++i){ int ny=p.first+dy[i],nx=p.second+dx[i]; if(ny<0 or y<=ny or nx<0 or x<=nx) continue; if(cells[ny][nx]!=prev_color) continue; que.push(make_pair(ny,nx)); cells[ny][nx]=color; } } return move(res); } inline void dfs(int num,vector<vector<char>>& cells){ if(num>min_res) return; for(int i=0; i<3; ++i){ if(num>=ans) return; if(cells[0][0]=="BGR"[i]) continue; char prev_color=cells[0][0]; vector<pii> ps=fill_cells(cells,"BGR"[i]); if(ps.size()==x*y) ans=num; else dfs(num+1,cells); for(const pii& p:ps) cells[p.first][p.second]=prev_color; } } void solve(){ while(true){ cin >> x >> y; if(x==0 and y==0) break; vector<vector<char>> cells(y,vector<char>(x)); rep(i,0,y) rep(j,0,x) cin >> cells[i][j]; ans=inf; min_res=0; while(ans==inf){ dfs(0,cells); ++min_res; } cout << ans << endl; } } int main(){ std::ios::sync_with_stdio(false); std::cin.tie(0); solve(); return 0; }
### Prompt Please formulate a Cpp solution to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define rep(i,x,y) for(int i=(x);i<(y);++i) #define debug(x) #x << "=" << (x) #ifdef DEBUG #define _GLIBCXX_DEBUG #define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl #else #define dump(x) #endif typedef long long int ll; typedef pair<int,int> pii; //template<typename T> using vec=std::vector<T>; const int inf=1<<30; const long long int infll=1LL<<58; const double eps=1e-9; const int dx[]={1,0,-1,0},dy[]={0,1,0,-1}; template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (const auto &v : vec) { os << v << ","; } os << "]"; return os; } int x,y,ans,min_res=0; inline vector<pii> fill_cells(vector<vector<char>>& cells,char color){ vector<pii> res; queue<pii> que; que.push(make_pair(0,0)); char prev_color=cells[0][0]; cells[0][0]=color; while(!que.empty()){ pii p=que.front(); que.pop(); res.push_back(p); for(int i=0; i<4; ++i){ int ny=p.first+dy[i],nx=p.second+dx[i]; if(ny<0 or y<=ny or nx<0 or x<=nx) continue; if(cells[ny][nx]!=prev_color) continue; que.push(make_pair(ny,nx)); cells[ny][nx]=color; } } return move(res); } inline void dfs(int num,vector<vector<char>>& cells){ if(num>min_res) return; for(int i=0; i<3; ++i){ if(num>=ans) return; if(cells[0][0]=="BGR"[i]) continue; char prev_color=cells[0][0]; vector<pii> ps=fill_cells(cells,"BGR"[i]); if(ps.size()==x*y) ans=num; else dfs(num+1,cells); for(const pii& p:ps) cells[p.first][p.second]=prev_color; } } void solve(){ while(true){ cin >> x >> y; if(x==0 and y==0) break; vector<vector<char>> cells(y,vector<char>(x)); rep(i,0,y) rep(j,0,x) cin >> cells[i][j]; ans=inf; min_res=0; while(ans==inf){ dfs(0,cells); ++min_res; } cout << ans << endl; } } int main(){ std::ios::sync_with_stdio(false); std::cin.tie(0); solve(); return 0; } ```
#include<iostream> #include<queue> using namespace std; struct grid{ char g[12][12]; int cnt; }; void paint(grid &gr,char C,char c,int x,int y){ if(gr.g[y][x] != C){ return; } gr.g[y][x] = c; paint(gr,C,c,x+1,y); paint(gr,C,c,x-1,y); paint(gr,C,c,x,y+1); paint(gr,C,c,x,y-1); } bool check(grid &gr,char C,int x,int y){ bool flag = true; for(int i=1;i<=y;i++){ for(int j=1;j<=x;j++){ if(C != gr.g[i][j]){ flag = false; break; } } } return flag; } int main(){ while(true){ int x,y; cin >> x >> y; if(x==0 && y==0){ break; } grid st; for(int i=1;i<=y;i++){ for(int j=1;j<=x;j++){ cin >> st.g[i][j]; } } st.cnt = 0; queue<grid> que; que.push(st); while(true){ grid data = que.front(); que.pop(); char C; C = data.g[1][1]; if(check(data,C,x,y)){ cout << data.cnt << endl; break; } if(C != 'R'){ grid tmp = data; tmp.cnt += 1; paint(tmp,C,'R',1,1); que.push(tmp); } if(C != 'G'){ grid tmp = data; tmp.cnt += 1; paint(tmp,C,'G',1,1); que.push(tmp); } if(C != 'B'){ grid tmp = data; tmp.cnt += 1; paint(tmp,C,'B',1,1); que.push(tmp); } } } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include<iostream> #include<queue> using namespace std; struct grid{ char g[12][12]; int cnt; }; void paint(grid &gr,char C,char c,int x,int y){ if(gr.g[y][x] != C){ return; } gr.g[y][x] = c; paint(gr,C,c,x+1,y); paint(gr,C,c,x-1,y); paint(gr,C,c,x,y+1); paint(gr,C,c,x,y-1); } bool check(grid &gr,char C,int x,int y){ bool flag = true; for(int i=1;i<=y;i++){ for(int j=1;j<=x;j++){ if(C != gr.g[i][j]){ flag = false; break; } } } return flag; } int main(){ while(true){ int x,y; cin >> x >> y; if(x==0 && y==0){ break; } grid st; for(int i=1;i<=y;i++){ for(int j=1;j<=x;j++){ cin >> st.g[i][j]; } } st.cnt = 0; queue<grid> que; que.push(st); while(true){ grid data = que.front(); que.pop(); char C; C = data.g[1][1]; if(check(data,C,x,y)){ cout << data.cnt << endl; break; } if(C != 'R'){ grid tmp = data; tmp.cnt += 1; paint(tmp,C,'R',1,1); que.push(tmp); } if(C != 'G'){ grid tmp = data; tmp.cnt += 1; paint(tmp,C,'G',1,1); que.push(tmp); } if(C != 'B'){ grid tmp = data; tmp.cnt += 1; paint(tmp,C,'B',1,1); que.push(tmp); } } } return 0; } ```
#include <cstdlib> #include <iostream> #include <queue> #include <vector> using namespace std; int w, h; constexpr int dx[] = {1, -1, 0, 0}; constexpr int dy[] = {0, 0, 1, -1}; inline bool out(int x, int y) { return x < 0 || y < 0 || x >= w || y >= h; } int count_panel(const vector<vector<int>> &field) { int res = 0; queue<pair<int, int>> que; que.push({0, 0}); vector<vector<bool>> visited(h, vector<bool>(w, false)); visited[0][0] = true; while(!que.empty()) { const int x = que.front().first; const int y = que.front().second; que.pop(); ++res; for(int d = 0; d < 4; ++d) { const int nx = x + dx[d]; const int ny = y + dy[d]; if(out(nx, ny)) continue; if(field[y][x] != field[ny][nx]) continue; if(visited[ny][nx]) continue; que.push({nx, ny}); visited[ny][nx] = true; } } return res; } void change(vector<vector<int>> &field, int to, int x = 0, int y = 0) { const int from = field[y][x]; field[y][x] = to; for(int d = 0; d < 4; ++d) { const int nx = x + dx[d]; const int ny = y + dy[d]; if(out(nx, ny) || field[ny][nx] != from) continue; change(field, to, nx, ny); } } bool dfs(int rest, int cnt, const vector<vector<int>> &field) { if(rest == 0) return false; for(int color = 0; color < 3; ++color) { if(color == field[0][0]) continue; auto next_field = field; change(next_field, color); const int next_cnt = count_panel(next_field); if(next_cnt == w * h) return true; if(next_cnt == cnt) continue; if(dfs(rest - 1, next_cnt, next_field)) return true; } return false; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int convert[256]; convert['R'] = 0; convert['G'] = 1; convert['B'] = 2; while(cin >> w >> h && w) { vector<vector<int>> field(h, vector<int>(w)); for(int i = 0; i < h; ++i) { for(int j = 0; j < w; ++j) { char c; cin >> c; field[i][j] = convert[c]; } } const int cnt = count_panel(field); if(cnt == h * w) { cout << 0 << endl; continue; } for(int ans = 1;; ++ans) { if(dfs(ans, cnt, field)) { cout << ans << endl; break; } } } return EXIT_SUCCESS; }
### Prompt Please formulate a cpp solution to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <cstdlib> #include <iostream> #include <queue> #include <vector> using namespace std; int w, h; constexpr int dx[] = {1, -1, 0, 0}; constexpr int dy[] = {0, 0, 1, -1}; inline bool out(int x, int y) { return x < 0 || y < 0 || x >= w || y >= h; } int count_panel(const vector<vector<int>> &field) { int res = 0; queue<pair<int, int>> que; que.push({0, 0}); vector<vector<bool>> visited(h, vector<bool>(w, false)); visited[0][0] = true; while(!que.empty()) { const int x = que.front().first; const int y = que.front().second; que.pop(); ++res; for(int d = 0; d < 4; ++d) { const int nx = x + dx[d]; const int ny = y + dy[d]; if(out(nx, ny)) continue; if(field[y][x] != field[ny][nx]) continue; if(visited[ny][nx]) continue; que.push({nx, ny}); visited[ny][nx] = true; } } return res; } void change(vector<vector<int>> &field, int to, int x = 0, int y = 0) { const int from = field[y][x]; field[y][x] = to; for(int d = 0; d < 4; ++d) { const int nx = x + dx[d]; const int ny = y + dy[d]; if(out(nx, ny) || field[ny][nx] != from) continue; change(field, to, nx, ny); } } bool dfs(int rest, int cnt, const vector<vector<int>> &field) { if(rest == 0) return false; for(int color = 0; color < 3; ++color) { if(color == field[0][0]) continue; auto next_field = field; change(next_field, color); const int next_cnt = count_panel(next_field); if(next_cnt == w * h) return true; if(next_cnt == cnt) continue; if(dfs(rest - 1, next_cnt, next_field)) return true; } return false; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int convert[256]; convert['R'] = 0; convert['G'] = 1; convert['B'] = 2; while(cin >> w >> h && w) { vector<vector<int>> field(h, vector<int>(w)); for(int i = 0; i < h; ++i) { for(int j = 0; j < w; ++j) { char c; cin >> c; field[i][j] = convert[c]; } } const int cnt = count_panel(field); if(cnt == h * w) { cout << 0 << endl; continue; } for(int ans = 1;; ++ans) { if(dfs(ans, cnt, field)) { cout << ans << endl; break; } } } return EXIT_SUCCESS; } ```
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <deque> #include <list> #include <queue> #include <stack> #include <vector> #include <utility> #include <algorithm> #include <map> #include <set> #include <complex> #include <cmath> #include <limits> #include <cfloat> #include <climits> #include <ctime> #include <cassert> #include <numeric> #include <functional> #include <bitset> using namespace std; #define int long long int const int INF = 1001001001001001LL; const int MOD = 1000000007; int di[4] = {1, 0, -1, 0}; int dj[4] = {0, -1, 0, 1}; int x, y; void dfs(int i, int j, char cur, char next, vector<vector<char>> &vec){ vec[i][j] = next; for(int h = 0; h < 4; h++){ int ni = i + di[h]; int nj = j + dj[h]; if(ni < 0 || y <= ni || nj < 0 || x <= nj) continue; if(vec[ni][nj] == cur) dfs(ni, nj, cur, next, vec); } } signed main(){ while(1){ cin >> x >> y; if(x == 0) break; vector<vector<char>> g(y, vector<char> (x)); for(int i = 0; i < y; i++){ for(int j = 0; j < x; j++){ cin >> g[i][j]; } } set<vector<vector<char>>> st; queue<pair<int, vector<vector<char>>>> que; que.push({0, g}); st.insert(g); while(1){ auto tmp = que.front(); auto vec = tmp.second; int cost = tmp.first; que.pop(); /*cout << endl; for(int i = 0; i < y; i++){ for(int j = 0; j < x; j++){ cout << vec[i][j] << " "; } cout << endl; } cout << endl; */ bool flag = true; char cur = vec[0][0]; for(int i = 0; i < y; i++){ for(int j = 0; j < x; j++){ if(vec[i][j] != cur) flag = false; } } if(flag){ cout << cost << endl; break; } if(cur != 'R'){ auto vec2 = vec; dfs(0, 0, cur, 'R', vec2); if(st.find(vec2) == st.end()){ que.push({cost + 1, vec2}); st.insert(vec2); } } if(cur != 'B'){ auto vec2 = vec; dfs(0, 0, cur, 'B', vec2); if(st.find(vec2) == st.end()){ que.push({cost + 1, vec2}); st.insert(vec2); } } if(cur != 'G'){ auto vec2 = vec; dfs(0, 0, cur, 'G', vec2); if(st.find(vec2) == st.end()){ que.push({cost + 1, vec2}); st.insert(vec2); } } } } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <deque> #include <list> #include <queue> #include <stack> #include <vector> #include <utility> #include <algorithm> #include <map> #include <set> #include <complex> #include <cmath> #include <limits> #include <cfloat> #include <climits> #include <ctime> #include <cassert> #include <numeric> #include <functional> #include <bitset> using namespace std; #define int long long int const int INF = 1001001001001001LL; const int MOD = 1000000007; int di[4] = {1, 0, -1, 0}; int dj[4] = {0, -1, 0, 1}; int x, y; void dfs(int i, int j, char cur, char next, vector<vector<char>> &vec){ vec[i][j] = next; for(int h = 0; h < 4; h++){ int ni = i + di[h]; int nj = j + dj[h]; if(ni < 0 || y <= ni || nj < 0 || x <= nj) continue; if(vec[ni][nj] == cur) dfs(ni, nj, cur, next, vec); } } signed main(){ while(1){ cin >> x >> y; if(x == 0) break; vector<vector<char>> g(y, vector<char> (x)); for(int i = 0; i < y; i++){ for(int j = 0; j < x; j++){ cin >> g[i][j]; } } set<vector<vector<char>>> st; queue<pair<int, vector<vector<char>>>> que; que.push({0, g}); st.insert(g); while(1){ auto tmp = que.front(); auto vec = tmp.second; int cost = tmp.first; que.pop(); /*cout << endl; for(int i = 0; i < y; i++){ for(int j = 0; j < x; j++){ cout << vec[i][j] << " "; } cout << endl; } cout << endl; */ bool flag = true; char cur = vec[0][0]; for(int i = 0; i < y; i++){ for(int j = 0; j < x; j++){ if(vec[i][j] != cur) flag = false; } } if(flag){ cout << cost << endl; break; } if(cur != 'R'){ auto vec2 = vec; dfs(0, 0, cur, 'R', vec2); if(st.find(vec2) == st.end()){ que.push({cost + 1, vec2}); st.insert(vec2); } } if(cur != 'B'){ auto vec2 = vec; dfs(0, 0, cur, 'B', vec2); if(st.find(vec2) == st.end()){ que.push({cost + 1, vec2}); st.insert(vec2); } } if(cur != 'G'){ auto vec2 = vec; dfs(0, 0, cur, 'G', vec2); if(st.find(vec2) == st.end()){ que.push({cost + 1, vec2}); st.insert(vec2); } } } } return 0; } ```
#include <stdio.h> #include <cmath> #include <algorithm> #include <cfloat> #include <stack> #include <queue> #include <vector> #include <string> #include <iostream> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; struct Info{ char map[10][10]; }; struct Data{ Data(int arg_row,int arg_col){ row = arg_row; col = arg_col; } int row,col; }; int H,W; int diff_row[4] = {-1,0,0,1},diff_col[4] = {0,-1,1,0}; char base_map[10][10]; bool FLG; char to_color[3] = {'R','G','B'}; bool rangeCheck(int row,int col){ if(row >= 0 && row <= H-1 && col >= 0 && col <= W-1)return true; else{ return false; } } void back_track(Info info,int depth,int max_depth){ if(FLG)return; bool is_cleared = true; for(int row = 0; row < H; row++){ for(int col = 0; col < W; col++){ if(info.map[row][col] != info.map[0][0]){ is_cleared = false; break; } } if(!is_cleared)break; } if(is_cleared){ FLG = true; return; } char pre_color = info.map[0][0]; queue<Data> Q; int adj_row,adj_col; for(int loop = 0; loop < 3; loop++){ if(to_color[loop] == pre_color)continue; Info next_info; for(int row = 0;row < H; row++){ for(int col = 0; col < W; col++){ next_info.map[row][col] = info.map[row][col]; } } next_info.map[0][0] = to_color[loop]; Q.push(Data(0,0)); while(!Q.empty()){ for(int i = 0; i < 4; i++){ adj_row = Q.front().row + diff_row[i]; adj_col = Q.front().col + diff_col[i]; if(!rangeCheck(adj_row,adj_col))continue; if(next_info.map[adj_row][adj_col] == pre_color){ next_info.map[adj_row][adj_col] = to_color[loop]; Q.push(Data(adj_row,adj_col)); } } Q.pop(); } if(depth < max_depth){ back_track(next_info,depth+1,max_depth); } } } void func(){ char buf[2]; for(int row = 0; row < H; row++){ for(int col = 0; col < W; col++){ scanf("%s",buf); base_map[row][col] = buf[0]; } } FLG = false; int ans = 0; for(int i = 0; ; i++){ Info info; for(int row = 0; row < H; row++){ for(int col = 0; col < W; col++){ info.map[row][col] = base_map[row][col]; } } back_track(info,0,i); if(FLG){ ans = i; break; } } printf("%d\n",ans); } int main(){ while(true){ scanf("%d %d",&W,&H); if(W == 0 && H == 0)break; func(); } return 0; }
### Prompt In Cpp, your task is to solve the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <stdio.h> #include <cmath> #include <algorithm> #include <cfloat> #include <stack> #include <queue> #include <vector> #include <string> #include <iostream> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; struct Info{ char map[10][10]; }; struct Data{ Data(int arg_row,int arg_col){ row = arg_row; col = arg_col; } int row,col; }; int H,W; int diff_row[4] = {-1,0,0,1},diff_col[4] = {0,-1,1,0}; char base_map[10][10]; bool FLG; char to_color[3] = {'R','G','B'}; bool rangeCheck(int row,int col){ if(row >= 0 && row <= H-1 && col >= 0 && col <= W-1)return true; else{ return false; } } void back_track(Info info,int depth,int max_depth){ if(FLG)return; bool is_cleared = true; for(int row = 0; row < H; row++){ for(int col = 0; col < W; col++){ if(info.map[row][col] != info.map[0][0]){ is_cleared = false; break; } } if(!is_cleared)break; } if(is_cleared){ FLG = true; return; } char pre_color = info.map[0][0]; queue<Data> Q; int adj_row,adj_col; for(int loop = 0; loop < 3; loop++){ if(to_color[loop] == pre_color)continue; Info next_info; for(int row = 0;row < H; row++){ for(int col = 0; col < W; col++){ next_info.map[row][col] = info.map[row][col]; } } next_info.map[0][0] = to_color[loop]; Q.push(Data(0,0)); while(!Q.empty()){ for(int i = 0; i < 4; i++){ adj_row = Q.front().row + diff_row[i]; adj_col = Q.front().col + diff_col[i]; if(!rangeCheck(adj_row,adj_col))continue; if(next_info.map[adj_row][adj_col] == pre_color){ next_info.map[adj_row][adj_col] = to_color[loop]; Q.push(Data(adj_row,adj_col)); } } Q.pop(); } if(depth < max_depth){ back_track(next_info,depth+1,max_depth); } } } void func(){ char buf[2]; for(int row = 0; row < H; row++){ for(int col = 0; col < W; col++){ scanf("%s",buf); base_map[row][col] = buf[0]; } } FLG = false; int ans = 0; for(int i = 0; ; i++){ Info info; for(int row = 0; row < H; row++){ for(int col = 0; col < W; col++){ info.map[row][col] = base_map[row][col]; } } back_track(info,0,i); if(FLG){ ans = i; break; } } printf("%d\n",ans); } int main(){ while(true){ scanf("%d %d",&W,&H); if(W == 0 && H == 0)break; func(); } return 0; } ```
#include<iostream> #include<cstdio> #include<cstring> #include<map> #include<queue> #include<algorithm> using namespace std; typedef pair<int,int> P; class state{ public: char f[11][11],c,l; state(){} state(int c,int l):c(c),l(l){}; }; char in[11][11]; state ne; bool used[11][11]; const int dx[4]={0,0,1,-1}; const int dy[4]={1,-1,0,0}; int n,m; int res; void dfs(int next,int prev,int x,int y){ if(x==0 && y==0){ memset(used,false,sizeof(used)); } ne.f[x][y]=next; used[x][y]=true; for(int i=0;i<4;i++){ int nx=x+dx[i],ny=y+dy[i]; if(nx>=0 && nx<n && ny>=0 && ny<m){ if(!used[nx][ny]){ used[nx][ny]=true; if(ne.f[nx][ny]==next){ } if(ne.f[nx][ny]==prev){ dfs(next,prev,nx,ny); } } } } } void cnt(int prev,int x,int y){ if(x==0 && y==0){ ne.l=0; memset(used,false,sizeof(used)); } ne.l++; used[x][y]=true; for(int i=0;i<4;i++){ int nx=x+dx[i],ny=y+dy[i]; if(nx>=0 && nx<n && ny>=0 && ny<m){ if(!used[nx][ny]){ used[nx][ny]=true; if(ne.f[nx][ny]==prev){ cnt(prev,nx,ny); } } } } } void bfs(state init){ queue<state> que; res=50; que.push(init); while(que.size()){ state p=que.front(); que.pop(); if(p.c>=res)break; ne.c=p.c+1; for(int i=0;i<=2;i++){ memcpy(ne.f,p.f,sizeof(ne.f)); dfs(i,ne.f[0][0],0,0); cnt(ne.f[0][0],0,0); if(ne.l==n*m)res=min(res,p.c+1); else if(ne.l>p.l)que.push(ne); } } } int main(void){ while(1){ res=100; scanf("%d%d\n",&n,&m); if(n==0 && m==0)break; state init=state(0,1); for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ cin >> in[j][i]; if(in[j][i]=='R')init.f[j][i]=0; if(in[j][i]=='G')init.f[j][i]=1; if(in[j][i]=='B')init.f[j][i]=2; } } memcpy(ne.f,init.f,sizeof(ne.f)); cnt(ne.f[0][0],0,0); if(ne.l==n*m)printf("0\n"); else{ bfs(init); printf("%d\n",res); } } return 0; }
### Prompt Create a solution in Cpp for the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include<iostream> #include<cstdio> #include<cstring> #include<map> #include<queue> #include<algorithm> using namespace std; typedef pair<int,int> P; class state{ public: char f[11][11],c,l; state(){} state(int c,int l):c(c),l(l){}; }; char in[11][11]; state ne; bool used[11][11]; const int dx[4]={0,0,1,-1}; const int dy[4]={1,-1,0,0}; int n,m; int res; void dfs(int next,int prev,int x,int y){ if(x==0 && y==0){ memset(used,false,sizeof(used)); } ne.f[x][y]=next; used[x][y]=true; for(int i=0;i<4;i++){ int nx=x+dx[i],ny=y+dy[i]; if(nx>=0 && nx<n && ny>=0 && ny<m){ if(!used[nx][ny]){ used[nx][ny]=true; if(ne.f[nx][ny]==next){ } if(ne.f[nx][ny]==prev){ dfs(next,prev,nx,ny); } } } } } void cnt(int prev,int x,int y){ if(x==0 && y==0){ ne.l=0; memset(used,false,sizeof(used)); } ne.l++; used[x][y]=true; for(int i=0;i<4;i++){ int nx=x+dx[i],ny=y+dy[i]; if(nx>=0 && nx<n && ny>=0 && ny<m){ if(!used[nx][ny]){ used[nx][ny]=true; if(ne.f[nx][ny]==prev){ cnt(prev,nx,ny); } } } } } void bfs(state init){ queue<state> que; res=50; que.push(init); while(que.size()){ state p=que.front(); que.pop(); if(p.c>=res)break; ne.c=p.c+1; for(int i=0;i<=2;i++){ memcpy(ne.f,p.f,sizeof(ne.f)); dfs(i,ne.f[0][0],0,0); cnt(ne.f[0][0],0,0); if(ne.l==n*m)res=min(res,p.c+1); else if(ne.l>p.l)que.push(ne); } } } int main(void){ while(1){ res=100; scanf("%d%d\n",&n,&m); if(n==0 && m==0)break; state init=state(0,1); for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ cin >> in[j][i]; if(in[j][i]=='R')init.f[j][i]=0; if(in[j][i]=='G')init.f[j][i]=1; if(in[j][i]=='B')init.f[j][i]=2; } } memcpy(ne.f,init.f,sizeof(ne.f)); cnt(ne.f[0][0],0,0); if(ne.l==n*m)printf("0\n"); else{ bfs(init); printf("%d\n",res); } } return 0; } ```
#include <iostream> using namespace std; int x,y,an,fc,dx[4]={1,0,-1,0},dy[4]={0,-1,0,1}; void fill(int (*d)[10],int c,int cr,int fx,int fy) { int i; for (i=0;i<4;i++) { if (fx+dx[i]>=0 && fx+dx[i]<x && fy+dy[i]>=0 && fy+dy[i]<y) { if (d[fy+dy[i]][fx+dx[i]]==c) { fc++; d[fy+dy[i]][fx+dx[i]]=cr; fill(d,c,cr,fx+dx[i],fy+dy[i]); } } } } int ans(int (*s)[10],int cr,int g) { int i,j,k,d[10][10]; for (i=1;i<3;i++) { fc=1; for (j=0;j<y;j++) for (k=0;k<x;k++) d[j][k]=s[j][k]; d[0][0]=(cr+i) % 3; fill(d,cr,(cr+i) % 3,0,0); if (fc==x*y) { an=g; return 1;} if (g<an) ans(d,(cr+i) % 3,g+1); } return 0; } int main() { char c; int i,j,s[10][10]; while(cin >> x >> y && x+y>0) { for (i=0;i<y;i++) for (j=0;j<x;j++) { cin >> c; if (c=='R') s[i][j]=0; if (c=='G') s[i][j]=1; if (c=='B') s[i][j]=2;} an=100; ans(s,s[0][0],0); cout << an << endl; } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <iostream> using namespace std; int x,y,an,fc,dx[4]={1,0,-1,0},dy[4]={0,-1,0,1}; void fill(int (*d)[10],int c,int cr,int fx,int fy) { int i; for (i=0;i<4;i++) { if (fx+dx[i]>=0 && fx+dx[i]<x && fy+dy[i]>=0 && fy+dy[i]<y) { if (d[fy+dy[i]][fx+dx[i]]==c) { fc++; d[fy+dy[i]][fx+dx[i]]=cr; fill(d,c,cr,fx+dx[i],fy+dy[i]); } } } } int ans(int (*s)[10],int cr,int g) { int i,j,k,d[10][10]; for (i=1;i<3;i++) { fc=1; for (j=0;j<y;j++) for (k=0;k<x;k++) d[j][k]=s[j][k]; d[0][0]=(cr+i) % 3; fill(d,cr,(cr+i) % 3,0,0); if (fc==x*y) { an=g; return 1;} if (g<an) ans(d,(cr+i) % 3,g+1); } return 0; } int main() { char c; int i,j,s[10][10]; while(cin >> x >> y && x+y>0) { for (i=0;i<y;i++) for (j=0;j<x;j++) { cin >> c; if (c=='R') s[i][j]=0; if (c=='G') s[i][j]=1; if (c=='B') s[i][j]=2;} an=100; ans(s,s[0][0],0); cout << an << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int x,y; struct sc{ char cs[11][11]; }; void DFS(sc& v,char base,char c,int xp,int yp){ if(xp < 0 || yp < 0 || xp >= x || yp >= y || v.cs[xp][yp]!=base){ return; } v.cs[xp][yp] = c; DFS(v,base,c,xp+1,yp); DFS(v,base,c,xp,yp+1); DFS(v,base,c,xp-1,yp); DFS(v,base,c,xp,yp-1); } int main() { while(1){ //scanf(" %d %d",&x,&y); cin >> x >> y; if(x==0 && y==0){ break; } sc v; //v.cs.resize(static_cast<unsigned>(x)); for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ //char c; /*scanf(" %c",&c); v.cs[j][i] = c;*/ cin >> v.cs[j][i]; //v.cs[j][i] = c; } } bool flag = true; for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ if(v.cs[0][0] != v.cs[j][i]){ flag = false; break; } } } if(flag){ cout << 0 << endl; continue; } queue<pair<sc,int > > q; q.push(make_pair(v,0)); while(!q.empty()){ int count = q.front().second; v = q.front().first; /*for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ cout << v.cs[i][j] << ' '; }cout << endl; }cout << endl;*/ q.pop(); count++; sc tmp; //tmp.resize(static_cast<unsigned>(x)); /*for(int i=0;i<y;i++){ tmp[i] += v.cs[i]; }*/ tmp = v; if(v.cs[0][0] != 'R'){ DFS(v,v.cs[0][0],'R',0,0); flag = true; for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ if('R' != v.cs[j][i]){ flag = false; break; } } } if(flag){ cout << count << endl; break; } q.push(make_pair(v,count)); /*for(int i=0;i<y;i++){ v[i] = tmp[i]; }*/ v = tmp; } if(v.cs[0][0] != 'G'){ DFS(v,v.cs[0][0],'G',0,0); flag = true; for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ if('G' != v.cs[j][i]){ flag = false; break; } } } if(flag){ cout << count << endl; break; } q.push(make_pair(v,count)); /*for(int i=0;i<y;i++){ v.cs[i] = tmp[i]; }*/ v = tmp; } if(v.cs[0][0] != 'B'){ //char base = v.cs[0][0]; DFS(v,v.cs[0][0],'B',0,0); flag = true; for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ if('B' != v.cs[j][i]){ flag = false; break; } } } if(flag){ cout << count << endl; break; } q.push(make_pair(v,count)); } } } return 0; }
### Prompt In cpp, your task is to solve the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int x,y; struct sc{ char cs[11][11]; }; void DFS(sc& v,char base,char c,int xp,int yp){ if(xp < 0 || yp < 0 || xp >= x || yp >= y || v.cs[xp][yp]!=base){ return; } v.cs[xp][yp] = c; DFS(v,base,c,xp+1,yp); DFS(v,base,c,xp,yp+1); DFS(v,base,c,xp-1,yp); DFS(v,base,c,xp,yp-1); } int main() { while(1){ //scanf(" %d %d",&x,&y); cin >> x >> y; if(x==0 && y==0){ break; } sc v; //v.cs.resize(static_cast<unsigned>(x)); for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ //char c; /*scanf(" %c",&c); v.cs[j][i] = c;*/ cin >> v.cs[j][i]; //v.cs[j][i] = c; } } bool flag = true; for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ if(v.cs[0][0] != v.cs[j][i]){ flag = false; break; } } } if(flag){ cout << 0 << endl; continue; } queue<pair<sc,int > > q; q.push(make_pair(v,0)); while(!q.empty()){ int count = q.front().second; v = q.front().first; /*for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ cout << v.cs[i][j] << ' '; }cout << endl; }cout << endl;*/ q.pop(); count++; sc tmp; //tmp.resize(static_cast<unsigned>(x)); /*for(int i=0;i<y;i++){ tmp[i] += v.cs[i]; }*/ tmp = v; if(v.cs[0][0] != 'R'){ DFS(v,v.cs[0][0],'R',0,0); flag = true; for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ if('R' != v.cs[j][i]){ flag = false; break; } } } if(flag){ cout << count << endl; break; } q.push(make_pair(v,count)); /*for(int i=0;i<y;i++){ v[i] = tmp[i]; }*/ v = tmp; } if(v.cs[0][0] != 'G'){ DFS(v,v.cs[0][0],'G',0,0); flag = true; for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ if('G' != v.cs[j][i]){ flag = false; break; } } } if(flag){ cout << count << endl; break; } q.push(make_pair(v,count)); /*for(int i=0;i<y;i++){ v.cs[i] = tmp[i]; }*/ v = tmp; } if(v.cs[0][0] != 'B'){ //char base = v.cs[0][0]; DFS(v,v.cs[0][0],'B',0,0); flag = true; for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ if('B' != v.cs[j][i]){ flag = false; break; } } } if(flag){ cout << count << endl; break; } q.push(make_pair(v,count)); } } } return 0; } ```
#include<bits/stdc++.h> #define rep(i,n)for(int i=0;i<(n);i++) using namespace std; struct st { char f[10][11]; int cnt; }; int w, h, dx[]{ 1,-1,0,0 }, dy[]{ 0,0,1,-1 }; char dir[] = "RGB"; bool ok(st&s) { char c = s.f[0][0]; rep(i, h)rep(j, w) { if (s.f[i][j] != c)return false; } return true; } void dfs(st&s, int x, int y, char b, char c) { s.f[x][y] = c; rep(i, 4) { int nx = x + dx[i], ny = y + dy[i]; if (0 <= nx&&nx < h && 0 <= ny&&ny < w&&s.f[nx][ny] == b) dfs(s, nx, ny, b, c); } } int main() { while (scanf("%d%d", &w, &h), h) { st in; in.cnt = 0; memset(in.f, 0, sizeof(in.f)); rep(i, h)rep(j, w)cin >> in.f[i][j]; queue<st>que; que.push(in); while (1) { st p = que.front(); que.pop(); if (ok(p)) { printf("%d\n", p.cnt); break; }st u; rep(i, 3) { u = p; if (dir[i] == u.f[0][0])continue; dfs(u, 0, 0, u.f[0][0], dir[i]); u.cnt++; que.push(u); } } } }
### Prompt Create a solution in cpp for the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include<bits/stdc++.h> #define rep(i,n)for(int i=0;i<(n);i++) using namespace std; struct st { char f[10][11]; int cnt; }; int w, h, dx[]{ 1,-1,0,0 }, dy[]{ 0,0,1,-1 }; char dir[] = "RGB"; bool ok(st&s) { char c = s.f[0][0]; rep(i, h)rep(j, w) { if (s.f[i][j] != c)return false; } return true; } void dfs(st&s, int x, int y, char b, char c) { s.f[x][y] = c; rep(i, 4) { int nx = x + dx[i], ny = y + dy[i]; if (0 <= nx&&nx < h && 0 <= ny&&ny < w&&s.f[nx][ny] == b) dfs(s, nx, ny, b, c); } } int main() { while (scanf("%d%d", &w, &h), h) { st in; in.cnt = 0; memset(in.f, 0, sizeof(in.f)); rep(i, h)rep(j, w)cin >> in.f[i][j]; queue<st>que; que.push(in); while (1) { st p = que.front(); que.pop(); if (ok(p)) { printf("%d\n", p.cnt); break; }st u; rep(i, 3) { u = p; if (dir[i] == u.f[0][0])continue; dfs(u, 0, 0, u.f[0][0], dir[i]); u.cnt++; que.push(u); } } } } ```
// 2014/09/11 Tazoe #include <iostream> #include <queue> #include <string> #include <map> using namespace std; struct State{ string G; int n; }; bool is_over(string G) { bool flg = true; for(int i=1; i<G.size(); i++){ if(G[i]!=G[0]){ flg = false; break; } } return flg; } void DFS(char g[12][12], char F, char T, int x, int y) { if(g[y][x]!=F) return; g[y][x] = T; DFS(g, F, T, x+1, y); DFS(g, F, T, x-1, y); DFS(g, F, T, x, y+1); DFS(g, F, T, x, y-1); } string fill(string G, char C, int X, int Y) { char g[12][12]; for(int y=0; y<Y+2; y++){ for(int x=0; x<X+2; x++){ g[y][x] = 'N'; } } for(int y=0; y<Y; y++){ for(int x=0; x<X; x++){ g[y+1][x+1] = G[y*X+x]; } } // for(int y=0; y<Y+2; y++){ // for(int x=0; x<X+2; x++){ // cout << g[y][x]; // } // cout << endl; // } DFS(g, G[0], C, 1, 1); for(int y=0; y<Y; y++){ for(int x=0; x<X; x++){ G[y*X+x] = g[y+1][x+1]; } } return G; } int main() { while(true){ int X, Y; cin >> X >> Y; if(X==0 && Y==0) break; string G = ""; for(int y=0; y<Y; y++){ for(int x=0; x<X; x++){ string C; cin >> C; G += C; } } // cout << G << endl; queue<struct State> que; struct State tmp; tmp.G = G; tmp.n = 0; que.push(tmp); map<string, bool> memo; while(!que.empty()){ tmp = que.front(); que.pop(); // cout << tmp.G << ' ' << tmp.n << endl; if(memo.find(tmp.G)!=memo.end()){ continue; } else{ memo[tmp.G] = true; } if(is_over(tmp.G) || tmp.n>100){ cout << tmp.n << endl; break; } if(tmp.G[0]=='R'){ struct State tmp2; tmp2.G = fill(tmp.G, 'G', X, Y); tmp2.n = tmp.n+1; que.push(tmp2); tmp2.G = fill(tmp.G, 'B', X, Y); que.push(tmp2); } else if(tmp.G[0]=='G'){ struct State tmp2; tmp2.G = fill(tmp.G, 'R', X, Y); tmp2.n = tmp.n+1; que.push(tmp2); tmp2.G = fill(tmp.G, 'B', X, Y); que.push(tmp2); } else if(tmp.G[0]=='B'){ struct State tmp2; tmp2.G = fill(tmp.G, 'R', X, Y); tmp2.n = tmp.n+1; que.push(tmp2); tmp2.G = fill(tmp.G, 'G', X, Y); que.push(tmp2); } } } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp // 2014/09/11 Tazoe #include <iostream> #include <queue> #include <string> #include <map> using namespace std; struct State{ string G; int n; }; bool is_over(string G) { bool flg = true; for(int i=1; i<G.size(); i++){ if(G[i]!=G[0]){ flg = false; break; } } return flg; } void DFS(char g[12][12], char F, char T, int x, int y) { if(g[y][x]!=F) return; g[y][x] = T; DFS(g, F, T, x+1, y); DFS(g, F, T, x-1, y); DFS(g, F, T, x, y+1); DFS(g, F, T, x, y-1); } string fill(string G, char C, int X, int Y) { char g[12][12]; for(int y=0; y<Y+2; y++){ for(int x=0; x<X+2; x++){ g[y][x] = 'N'; } } for(int y=0; y<Y; y++){ for(int x=0; x<X; x++){ g[y+1][x+1] = G[y*X+x]; } } // for(int y=0; y<Y+2; y++){ // for(int x=0; x<X+2; x++){ // cout << g[y][x]; // } // cout << endl; // } DFS(g, G[0], C, 1, 1); for(int y=0; y<Y; y++){ for(int x=0; x<X; x++){ G[y*X+x] = g[y+1][x+1]; } } return G; } int main() { while(true){ int X, Y; cin >> X >> Y; if(X==0 && Y==0) break; string G = ""; for(int y=0; y<Y; y++){ for(int x=0; x<X; x++){ string C; cin >> C; G += C; } } // cout << G << endl; queue<struct State> que; struct State tmp; tmp.G = G; tmp.n = 0; que.push(tmp); map<string, bool> memo; while(!que.empty()){ tmp = que.front(); que.pop(); // cout << tmp.G << ' ' << tmp.n << endl; if(memo.find(tmp.G)!=memo.end()){ continue; } else{ memo[tmp.G] = true; } if(is_over(tmp.G) || tmp.n>100){ cout << tmp.n << endl; break; } if(tmp.G[0]=='R'){ struct State tmp2; tmp2.G = fill(tmp.G, 'G', X, Y); tmp2.n = tmp.n+1; que.push(tmp2); tmp2.G = fill(tmp.G, 'B', X, Y); que.push(tmp2); } else if(tmp.G[0]=='G'){ struct State tmp2; tmp2.G = fill(tmp.G, 'R', X, Y); tmp2.n = tmp.n+1; que.push(tmp2); tmp2.G = fill(tmp.G, 'B', X, Y); que.push(tmp2); } else if(tmp.G[0]=='B'){ struct State tmp2; tmp2.G = fill(tmp.G, 'R', X, Y); tmp2.n = tmp.n+1; que.push(tmp2); tmp2.G = fill(tmp.G, 'G', X, Y); que.push(tmp2); } } } return 0; } ```
#include<cstdio> #include<cstdlib> #include<ctime> #include<cmath> #include<cstring> #include<cctype> #include<complex> #include<iostream> #include<sstream> #include<algorithm> #include<functional> #include<vector> #include<string> #include<stack> #include<queue> #include<map> #include<set> #include<bitset> #include<numeric> using namespace std; const int dx[]={1,0,-1,0}; const int dy[]={0,1,0,-1}; #define INF 1e+8 #define EPS 1e-7 #define PB push_back #define fi first #define se second #define ll long long #define reps(i,j,k) for(int i = (j); i < (k); i++) #define rep(i,j) reps(i,0,j) typedef pair<int,int> Pii; typedef vector<int> vi; class data{ public: short field[10][10]; short cnt; data(){} data(short _field[10][10],short _cnt){ rep(i,10){ rep(j,10){ field[i][j] = _field[i][j]; } } cnt = _cnt; } }; bool memo[10][10]; short chenge(char c){ if(c == 'R')return 0; if(c == 'G')return 1; return 2; } bool check(short f[10][10],short W,short H){ short comp = f[0][0]; rep(i,H){ rep(j,W){ if(comp != f[i][j])return false; } } return true; } void dfs(short b,short col,short y,short x,short stage[10][10],short W,short H){ rep(i,4){ short ny = y+dy[i]; short nx = x+dx[i]; if(ny < 0 || ny > H-1 || nx < 0 || nx > W-1 || stage[ny][nx]!=b)continue; if(memo[ny][nx]++)continue; stage[ny][nx] = col; dfs(b,col,ny,nx,stage,W,H); } return ; } void copy(short stage[10][10],short bef[10][10],short W,short H){ rep(i,H){ rep(j,W){ stage[i][j] = bef[i][j]; } } return ; } int main(){ int W,H; while(scanf("%d%d",&W,&H),W){ short stage[10][10]; rep(i,H){ rep(j,W){ char c; scanf(" %c",&c); stage[i][j] = chenge(c); } } queue < data > Q; Q.push(data(stage,0)); short ans = -1; while(!Q.empty()){ data d = Q.front();Q.pop(); /* puts("-----------------------------"); rep(i,H){ rep(j,W){ printf("%d ",d.field[i][j]); } puts(""); } puts("-----------------------------");*/ if(check(d.field,W,H)){ ans = d.cnt; break; } short col1 = (d.field[0][0]+1)%3; short col2 = (d.field[0][0]+2)%3; short sub[10][10]; copy(sub,d.field,W,H); memset(memo,0,sizeof(memo)); sub[0][0] = col1; dfs(d.field[0][0],col1,0,0,sub,W,H); Q.push(data(sub,d.cnt+1)); copy(sub,d.field,W,H); memset(memo,0,sizeof(memo)); sub[0][0] = col2; dfs(d.field[0][0],col2,0,0,sub,W,H); Q.push(data(sub,d.cnt+1)); } printf("%d\n",ans); } return 0; }
### Prompt In CPP, your task is to solve the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include<cstdio> #include<cstdlib> #include<ctime> #include<cmath> #include<cstring> #include<cctype> #include<complex> #include<iostream> #include<sstream> #include<algorithm> #include<functional> #include<vector> #include<string> #include<stack> #include<queue> #include<map> #include<set> #include<bitset> #include<numeric> using namespace std; const int dx[]={1,0,-1,0}; const int dy[]={0,1,0,-1}; #define INF 1e+8 #define EPS 1e-7 #define PB push_back #define fi first #define se second #define ll long long #define reps(i,j,k) for(int i = (j); i < (k); i++) #define rep(i,j) reps(i,0,j) typedef pair<int,int> Pii; typedef vector<int> vi; class data{ public: short field[10][10]; short cnt; data(){} data(short _field[10][10],short _cnt){ rep(i,10){ rep(j,10){ field[i][j] = _field[i][j]; } } cnt = _cnt; } }; bool memo[10][10]; short chenge(char c){ if(c == 'R')return 0; if(c == 'G')return 1; return 2; } bool check(short f[10][10],short W,short H){ short comp = f[0][0]; rep(i,H){ rep(j,W){ if(comp != f[i][j])return false; } } return true; } void dfs(short b,short col,short y,short x,short stage[10][10],short W,short H){ rep(i,4){ short ny = y+dy[i]; short nx = x+dx[i]; if(ny < 0 || ny > H-1 || nx < 0 || nx > W-1 || stage[ny][nx]!=b)continue; if(memo[ny][nx]++)continue; stage[ny][nx] = col; dfs(b,col,ny,nx,stage,W,H); } return ; } void copy(short stage[10][10],short bef[10][10],short W,short H){ rep(i,H){ rep(j,W){ stage[i][j] = bef[i][j]; } } return ; } int main(){ int W,H; while(scanf("%d%d",&W,&H),W){ short stage[10][10]; rep(i,H){ rep(j,W){ char c; scanf(" %c",&c); stage[i][j] = chenge(c); } } queue < data > Q; Q.push(data(stage,0)); short ans = -1; while(!Q.empty()){ data d = Q.front();Q.pop(); /* puts("-----------------------------"); rep(i,H){ rep(j,W){ printf("%d ",d.field[i][j]); } puts(""); } puts("-----------------------------");*/ if(check(d.field,W,H)){ ans = d.cnt; break; } short col1 = (d.field[0][0]+1)%3; short col2 = (d.field[0][0]+2)%3; short sub[10][10]; copy(sub,d.field,W,H); memset(memo,0,sizeof(memo)); sub[0][0] = col1; dfs(d.field[0][0],col1,0,0,sub,W,H); Q.push(data(sub,d.cnt+1)); copy(sub,d.field,W,H); memset(memo,0,sizeof(memo)); sub[0][0] = col2; dfs(d.field[0][0],col2,0,0,sub,W,H); Q.push(data(sub,d.cnt+1)); } printf("%d\n",ans); } return 0; } ```
#include <iostream> #include <queue> using namespace std; int w, h; int dx[] = { 0, 1, 0, -1 }; int dy[] = { -1, 0, 1, 0 }; struct Pazzle { char glid[10][10]; }; void draw(char glid[][10], int x, int y, char base, char color, int *count) { glid[y][x] = color; for (int i = 0; i < 4; i++) { int nposx = x + dx[i]; int nposy = y + dy[i]; if (nposx < 0 || nposx >= w || nposy < 0 || nposy >= h) { continue; } if (glid[nposy][nposx] == base) { (*count)++; draw(glid, nposx, nposy, base, color, count); } } } int main() { while (1) { Pazzle p; cin >> w >> h; if (w == 0) { break; } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> p.glid[i][j]; } } queue<Pazzle> que; int depth = 0; bool success = false; que.push(p); while (!que.empty()) { int size = que.size(); for (int i = 0; i < size; i++) { p = que.front(); que.pop(); int ret1 = 1, ret2 = 1; Pazzle temp1, temp2; temp1 = temp2 = p; if (p.glid[0][0] == 'R') { draw(temp1.glid, 0, 0, p.glid[0][0], 'G', &ret1); draw(temp2.glid, 0, 0, p.glid[0][0], 'B', &ret2); } else if (p.glid[0][0] == 'G') { draw(temp1.glid, 0, 0, p.glid[0][0], 'R', &ret1); draw(temp2.glid, 0, 0, p.glid[0][0], 'B', &ret2); } else { draw(temp1.glid, 0, 0, p.glid[0][0], 'R', &ret1); draw(temp2.glid, 0, 0, p.glid[0][0], 'G', &ret2); } if (ret1 == h * w || ret2 == h * w) {; success = true; break; } que.push(temp1); que.push(temp2); } if (success == true) { cout << depth << endl; break; } depth++; } } return 0; }
### Prompt Generate a CPP solution to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <iostream> #include <queue> using namespace std; int w, h; int dx[] = { 0, 1, 0, -1 }; int dy[] = { -1, 0, 1, 0 }; struct Pazzle { char glid[10][10]; }; void draw(char glid[][10], int x, int y, char base, char color, int *count) { glid[y][x] = color; for (int i = 0; i < 4; i++) { int nposx = x + dx[i]; int nposy = y + dy[i]; if (nposx < 0 || nposx >= w || nposy < 0 || nposy >= h) { continue; } if (glid[nposy][nposx] == base) { (*count)++; draw(glid, nposx, nposy, base, color, count); } } } int main() { while (1) { Pazzle p; cin >> w >> h; if (w == 0) { break; } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> p.glid[i][j]; } } queue<Pazzle> que; int depth = 0; bool success = false; que.push(p); while (!que.empty()) { int size = que.size(); for (int i = 0; i < size; i++) { p = que.front(); que.pop(); int ret1 = 1, ret2 = 1; Pazzle temp1, temp2; temp1 = temp2 = p; if (p.glid[0][0] == 'R') { draw(temp1.glid, 0, 0, p.glid[0][0], 'G', &ret1); draw(temp2.glid, 0, 0, p.glid[0][0], 'B', &ret2); } else if (p.glid[0][0] == 'G') { draw(temp1.glid, 0, 0, p.glid[0][0], 'R', &ret1); draw(temp2.glid, 0, 0, p.glid[0][0], 'B', &ret2); } else { draw(temp1.glid, 0, 0, p.glid[0][0], 'R', &ret1); draw(temp2.glid, 0, 0, p.glid[0][0], 'G', &ret2); } if (ret1 == h * w || ret2 == h * w) {; success = true; break; } que.push(temp1); que.push(temp2); } if (success == true) { cout << depth << endl; break; } depth++; } } return 0; } ```
#include<algorithm> #include<functional> #include<cmath> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<sstream> #include<iostream> #include<iomanip> #include<vector> #include<list> #include<stack> #include<queue> #include<map> #include<set> #include<bitset> #include<climits> #define all(c) (c).begin(), (c).end() #define rep(i,n) for(int i=0;i<(n);i++) #define pb(e) push_back(e) #define mp(a, b) make_pair(a, b) #define fr first #define sc second typedef unsigned long long UInt64; typedef long long Int64; const int INF=100000000; int dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; using namespace std; typedef pair<int ,int > P; int ans=INF; int x,y; inline int change(string ss) { if(ss=="R") return 0; if(ss=="G") return 1; if(ss=="B") return 2; } inline bool check(vector<vector<int> > tile) { rep(i,tile.size()) { rep(j,tile[i].size()) { if(tile[0][0]!=tile[i][j]) return false; } } return true; } inline void f(vector<vector<int> > &tile,int color) { //x,y queue<P> que; int oldcolor=tile[0][0]; tile[0][0]=color; que.push(P(0,0)); while(que.size()) { P p = que.front(); que.pop(); rep(i,4) { int nx=p.fr+dx[i]; int ny=p.sc+dy[i]; if(0<=nx&&nx<x&&0<=ny&&ny<y&&tile[nx][ny]==oldcolor) { tile[nx][ny]=color; que.push(P(nx,ny)); } } } } inline void dfs(vector<vector<int> > tile,int depth) { if(check(tile)) { ans=min(ans,depth); return; } //最大でも18手を越えないらしい if(depth==17) return; vector<vector<int> > tile1=tile; vector<vector<int> > tile2=tile; if(tile[0][0]==0) { f(tile1,1); f(tile2,2); } if(tile[0][0]==1) { f(tile1,0); f(tile2,2); } if(tile[0][0]==2) { f(tile1,1); f(tile2,0); } dfs(tile1,depth+1); dfs(tile2,depth+1); } inline void solve() { vector<vector<int> > tile; tile.reserve(10); rep(i,x) { vector<int> vec; vec.reserve(10); rep(j,y) { string c; cin>>c; vec.pb(change(c)); } tile.pb(vec); } dfs(tile,0); } int main() { while(cin>>x>>y) { swap(x,y); if(x==0&&y==0) return 0; solve(); cout<<ans<<endl; ans=INF; } return 0; }
### Prompt In CPP, your task is to solve the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include<algorithm> #include<functional> #include<cmath> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<sstream> #include<iostream> #include<iomanip> #include<vector> #include<list> #include<stack> #include<queue> #include<map> #include<set> #include<bitset> #include<climits> #define all(c) (c).begin(), (c).end() #define rep(i,n) for(int i=0;i<(n);i++) #define pb(e) push_back(e) #define mp(a, b) make_pair(a, b) #define fr first #define sc second typedef unsigned long long UInt64; typedef long long Int64; const int INF=100000000; int dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; using namespace std; typedef pair<int ,int > P; int ans=INF; int x,y; inline int change(string ss) { if(ss=="R") return 0; if(ss=="G") return 1; if(ss=="B") return 2; } inline bool check(vector<vector<int> > tile) { rep(i,tile.size()) { rep(j,tile[i].size()) { if(tile[0][0]!=tile[i][j]) return false; } } return true; } inline void f(vector<vector<int> > &tile,int color) { //x,y queue<P> que; int oldcolor=tile[0][0]; tile[0][0]=color; que.push(P(0,0)); while(que.size()) { P p = que.front(); que.pop(); rep(i,4) { int nx=p.fr+dx[i]; int ny=p.sc+dy[i]; if(0<=nx&&nx<x&&0<=ny&&ny<y&&tile[nx][ny]==oldcolor) { tile[nx][ny]=color; que.push(P(nx,ny)); } } } } inline void dfs(vector<vector<int> > tile,int depth) { if(check(tile)) { ans=min(ans,depth); return; } //最大でも18手を越えないらしい if(depth==17) return; vector<vector<int> > tile1=tile; vector<vector<int> > tile2=tile; if(tile[0][0]==0) { f(tile1,1); f(tile2,2); } if(tile[0][0]==1) { f(tile1,0); f(tile2,2); } if(tile[0][0]==2) { f(tile1,1); f(tile2,0); } dfs(tile1,depth+1); dfs(tile2,depth+1); } inline void solve() { vector<vector<int> > tile; tile.reserve(10); rep(i,x) { vector<int> vec; vec.reserve(10); rep(j,y) { string c; cin>>c; vec.pb(change(c)); } tile.pb(vec); } dfs(tile,0); } int main() { while(cin>>x>>y) { swap(x,y); if(x==0&&y==0) return 0; solve(); cout<<ans<<endl; ans=INF; } return 0; } ```
#include <iostream> #include <complex> #include <sstream> #include <string> #include <algorithm> #include <deque> #include <list> #include <map> #include <numeric> #include <queue> #include <vector> #include <set> #include <limits> #include <cstdio> #include <cctype> #include <cmath> #include <cstring> #include <cstdlib> #include <ctime> using namespace std; #define REP(i, j) for(int i = 0; i < (int)(j); ++i) #define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i) #define P pair<int, int> #define SORT(v) sort((v).begin(), (v).end()) #define REVERSE(v) reverse((v).begin(), (v).end()) const int MAX_Y = 11; const int MAX_X = 11; const int INF = 1e9 + 7; int Y, X, ans; int my[] = {0, 1, 0, -1}; int mx[] = {1, 0, -1, 0}; void change_color(int y, int x, int color, int bef_color, int v[MAX_Y][MAX_X]){ //cout <<y <<", " <<x <<endl; v[y][x] = color; REP(i, 4){ int ny = y + my[i], nx = x + mx[i]; if(ny >= 0 && nx >= 0 && ny < Y && nx < X && v[ny][nx] == bef_color) change_color(ny, nx, color, bef_color, v); } } bool isOK(int v[MAX_Y][MAX_X]){ REP(i, Y) REP(j, X) if(v[i][j] != v[0][0]) return false; return true; } int cntColor(int y, int x, int v[MAX_Y][MAX_X], int visited[MAX_Y][MAX_X]){ int ret = 1; visited[y][x] = true; REP(i, 2){ int ny = y + my[i], nx = x + mx[i]; if(ny >= 0 && nx >= 0 && ny < Y && nx < X && v[ny][nx] == v[0][0] && !visited[ny][nx]) ret += cntColor(ny, nx, v, visited); } return ret; } void dfs(int cnt, int all_cnt, int v[MAX_Y][MAX_X]){ //cout <<"--------" <<endl; //cout <<cnt <<", " <<isOK(v) <<endl; //REP(i, Y){ // REP(j, X) cout <<v[i][j] <<" "; // cout <<endl; //} if(isOK(v)) ans = cnt; if(cnt >= ans) return ; REP(i, 3){ if(v[0][0] == i) continue; //cout <<i <<"->" <<endl; int tmp[MAX_Y][MAX_X]; REP(i, Y) memcpy(tmp[i], v[i], sizeof(v[i])); change_color(0, 0, i, v[0][0], tmp); int tmptmp[MAX_X][MAX_Y] = {0}; int c = cntColor(0, 0, tmp, tmptmp); //REP(i, Y){ // REP(j, X) cout <<tmp[i][j] <<" "; // cout <<endl; //} if(c > all_cnt) dfs(cnt + 1, c, tmp); } } int main(){ int v[MAX_Y][MAX_X]; while(cin >>X >>Y && Y){ ans = INF; REP(i, Y){ REP(j, X){ char tmp; cin >>tmp; if(tmp == 'R') v[i][j] = 0; if(tmp == 'G') v[i][j] = 1; if(tmp == 'B') v[i][j] = 2; } } int tmp[MAX_X][MAX_Y] = {0}; REP(i, 3) dfs(0, cntColor(0, 0, v, tmp), v); cout <<ans <<endl; } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <iostream> #include <complex> #include <sstream> #include <string> #include <algorithm> #include <deque> #include <list> #include <map> #include <numeric> #include <queue> #include <vector> #include <set> #include <limits> #include <cstdio> #include <cctype> #include <cmath> #include <cstring> #include <cstdlib> #include <ctime> using namespace std; #define REP(i, j) for(int i = 0; i < (int)(j); ++i) #define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i) #define P pair<int, int> #define SORT(v) sort((v).begin(), (v).end()) #define REVERSE(v) reverse((v).begin(), (v).end()) const int MAX_Y = 11; const int MAX_X = 11; const int INF = 1e9 + 7; int Y, X, ans; int my[] = {0, 1, 0, -1}; int mx[] = {1, 0, -1, 0}; void change_color(int y, int x, int color, int bef_color, int v[MAX_Y][MAX_X]){ //cout <<y <<", " <<x <<endl; v[y][x] = color; REP(i, 4){ int ny = y + my[i], nx = x + mx[i]; if(ny >= 0 && nx >= 0 && ny < Y && nx < X && v[ny][nx] == bef_color) change_color(ny, nx, color, bef_color, v); } } bool isOK(int v[MAX_Y][MAX_X]){ REP(i, Y) REP(j, X) if(v[i][j] != v[0][0]) return false; return true; } int cntColor(int y, int x, int v[MAX_Y][MAX_X], int visited[MAX_Y][MAX_X]){ int ret = 1; visited[y][x] = true; REP(i, 2){ int ny = y + my[i], nx = x + mx[i]; if(ny >= 0 && nx >= 0 && ny < Y && nx < X && v[ny][nx] == v[0][0] && !visited[ny][nx]) ret += cntColor(ny, nx, v, visited); } return ret; } void dfs(int cnt, int all_cnt, int v[MAX_Y][MAX_X]){ //cout <<"--------" <<endl; //cout <<cnt <<", " <<isOK(v) <<endl; //REP(i, Y){ // REP(j, X) cout <<v[i][j] <<" "; // cout <<endl; //} if(isOK(v)) ans = cnt; if(cnt >= ans) return ; REP(i, 3){ if(v[0][0] == i) continue; //cout <<i <<"->" <<endl; int tmp[MAX_Y][MAX_X]; REP(i, Y) memcpy(tmp[i], v[i], sizeof(v[i])); change_color(0, 0, i, v[0][0], tmp); int tmptmp[MAX_X][MAX_Y] = {0}; int c = cntColor(0, 0, tmp, tmptmp); //REP(i, Y){ // REP(j, X) cout <<tmp[i][j] <<" "; // cout <<endl; //} if(c > all_cnt) dfs(cnt + 1, c, tmp); } } int main(){ int v[MAX_Y][MAX_X]; while(cin >>X >>Y && Y){ ans = INF; REP(i, Y){ REP(j, X){ char tmp; cin >>tmp; if(tmp == 'R') v[i][j] = 0; if(tmp == 'G') v[i][j] = 1; if(tmp == 'B') v[i][j] = 2; } } int tmp[MAX_X][MAX_Y] = {0}; REP(i, 3) dfs(0, cntColor(0, 0, v, tmp), v); cout <<ans <<endl; } return 0; } ```
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define reps(i,f,n) for(int i=f; i<int(n); ++i) #define rep(i,n) reps(i,0,n) typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; const int INF = 1001001001; const double EPS = 1e-10; const int dy[] = {-1, 0, 1, 0}; const int dx[] = {0, -1, 0, 1}; int h, w; int board[10][10]; int label[10][10]; vi color; vvi G; void labeling(int y, int x, int id) { label[y][x] = id; rep(i, 4){ int py = y + dy[i]; int px = x + dx[i]; if(py<0 || h<=py || px<0 || w<=px) continue; if(board[py][px] == board[y][x] && label[py][px] == -1) labeling(py, px, id); } } bool dfs(vi cand, int now, int depth) { if(depth == 0){ rep(i, cand.size()) if(cand[i] != 2) return false; return true; } vi tmp = cand; rep(c, 3){ if(c == now) continue; bool found = false; rep(i, cand.size()){ if(cand[i]==1 && color[i]==c){ rep(j, G[i].size()){ if(cand[G[i][j]] == 0) cand[G[i][j]] = 1; } cand[i] = 2; found = true; } } if(found){ if(dfs(cand, c, depth-1)) return true; cand = tmp; } } return false; } int main() { while(scanf("%d%d", &w, &h), w){ rep(i, h) rep(j, w){ char c[5]; scanf("%s", c); if(c[0] == 'R') board[i][j] = 0; else if(c[0] == 'G') board[i][j] = 1; else board[i][j] = 2; } rep(i, h) fill(label[i], label[i]+w, -1); color.clear(); rep(i, h) rep(j, w){ if(label[i][j] == -1){ labeling(i, j, color.size()); color.push_back(board[i][j]); } } G.assign(color.size(), vi()); rep(i, h) rep(j, w){ rep(k, 4){ int py = i + dy[k]; int px = j + dx[k]; if(py<0 || h<=py || px<0 || w<=px) continue; if(label[i][j] != label[py][px]){ G[label[i][j]].push_back(label[py][px]); G[label[py][px]].push_back(label[i][j]); } } } rep(i, G.size()){ sort(G[i].begin(), G[i].end()); G[i].erase(unique(G[i].begin(), G[i].end()), G[i].end()); } for(int depth=0;; ++depth){ vi cand(G.size(), 0); rep(i, G[0].size()) cand[G[0][i]] = 1; cand[0] = 2; if(dfs(cand, color[0], depth)){ printf("%d\n", depth); break; } } } return 0; }
### Prompt Please create a solution in CPP to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define reps(i,f,n) for(int i=f; i<int(n); ++i) #define rep(i,n) reps(i,0,n) typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; const int INF = 1001001001; const double EPS = 1e-10; const int dy[] = {-1, 0, 1, 0}; const int dx[] = {0, -1, 0, 1}; int h, w; int board[10][10]; int label[10][10]; vi color; vvi G; void labeling(int y, int x, int id) { label[y][x] = id; rep(i, 4){ int py = y + dy[i]; int px = x + dx[i]; if(py<0 || h<=py || px<0 || w<=px) continue; if(board[py][px] == board[y][x] && label[py][px] == -1) labeling(py, px, id); } } bool dfs(vi cand, int now, int depth) { if(depth == 0){ rep(i, cand.size()) if(cand[i] != 2) return false; return true; } vi tmp = cand; rep(c, 3){ if(c == now) continue; bool found = false; rep(i, cand.size()){ if(cand[i]==1 && color[i]==c){ rep(j, G[i].size()){ if(cand[G[i][j]] == 0) cand[G[i][j]] = 1; } cand[i] = 2; found = true; } } if(found){ if(dfs(cand, c, depth-1)) return true; cand = tmp; } } return false; } int main() { while(scanf("%d%d", &w, &h), w){ rep(i, h) rep(j, w){ char c[5]; scanf("%s", c); if(c[0] == 'R') board[i][j] = 0; else if(c[0] == 'G') board[i][j] = 1; else board[i][j] = 2; } rep(i, h) fill(label[i], label[i]+w, -1); color.clear(); rep(i, h) rep(j, w){ if(label[i][j] == -1){ labeling(i, j, color.size()); color.push_back(board[i][j]); } } G.assign(color.size(), vi()); rep(i, h) rep(j, w){ rep(k, 4){ int py = i + dy[k]; int px = j + dx[k]; if(py<0 || h<=py || px<0 || w<=px) continue; if(label[i][j] != label[py][px]){ G[label[i][j]].push_back(label[py][px]); G[label[py][px]].push_back(label[i][j]); } } } rep(i, G.size()){ sort(G[i].begin(), G[i].end()); G[i].erase(unique(G[i].begin(), G[i].end()), G[i].end()); } for(int depth=0;; ++depth){ vi cand(G.size(), 0); rep(i, G[0].size()) cand[G[0][i]] = 1; cand[0] = 2; if(dfs(cand, color[0], depth)){ printf("%d\n", depth); break; } } } return 0; } ```
# include "bits/stdc++.h" using namespace std; using LL = long long; using ULL = unsigned long long; const double PI = acos(-1); template<class T>constexpr T INF() { return ::std::numeric_limits<T>::max(); } template<class T>constexpr T HINF() { return INF<T>() / 2; } template <typename T_char>T_char TL(T_char cX) { return tolower(cX); }; template <typename T_char>T_char TU(T_char cX) { return toupper(cX); }; const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 }; const int dx[4] = { -1,0,1,0 }, dy[4] = { 0,-1,0,1 }; int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; } int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; } int d_cnt(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; } LL gcd(LL a, LL b) { if (b == 0)return a; return gcd(b, a%b); }; LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g*b; }; # define ALL(qpqpq) (qpqpq).begin(),(qpqpq).end() # define UNIQUE(wpwpw) (wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end()) # define LOWER(epepe) transform(ALL((epepe)),(epepe).begin(),TL<char>) # define UPPER(rprpr) transform(ALL((rprpr)),(rprpr).begin(),TU<char>) # define FOR(i,tptpt,ypypy) for(LL i=(tptpt);i<(ypypy);i++) # define REP(i,upupu) FOR(i,0,upupu) # define INIT std::ios::sync_with_stdio(false);std::cin.tie(0) # pragma warning(disable:4996) int h, w; struct st { char c[11][11]; int cnt; }; void dfs(st &s, int i, int j, char before,char after) { s.c[i][j] = after; REP(k, 4) { int ni = i + dy[k], nj = j + dx[k]; if (0 <= ni&&ni < h && 0 <= nj&&nj < w&&s.c[ni][nj] == before) { dfs(s, ni, nj, before, after); } } } bool isok(st &s) { REP(i, h)REP(j, w) { if (s.c[i][j] != s.c[0][0])return false; } return true; } char dir[] = "RGB"; void solve() { st s; s.cnt = 0; REP(i, h)REP(j, w)cin >> s.c[i][j]; queue<st>que; que.push(s); while (que.size()) { st q = que.front(); que.pop(); if (isok(q)) { cout << q.cnt << endl; return; } st u; REP(i, 3) { u = q; if (dir[i] == u.c[0][0])continue; dfs(u, 0, 0, u.c[0][0], dir[i]); u.cnt++; que.push(u); } } } int main() { while (cin >> w >> h&&w&&h) { solve(); } }
### Prompt Please formulate a cpp solution to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp # include "bits/stdc++.h" using namespace std; using LL = long long; using ULL = unsigned long long; const double PI = acos(-1); template<class T>constexpr T INF() { return ::std::numeric_limits<T>::max(); } template<class T>constexpr T HINF() { return INF<T>() / 2; } template <typename T_char>T_char TL(T_char cX) { return tolower(cX); }; template <typename T_char>T_char TU(T_char cX) { return toupper(cX); }; const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 }; const int dx[4] = { -1,0,1,0 }, dy[4] = { 0,-1,0,1 }; int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; } int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; } int d_cnt(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; } LL gcd(LL a, LL b) { if (b == 0)return a; return gcd(b, a%b); }; LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g*b; }; # define ALL(qpqpq) (qpqpq).begin(),(qpqpq).end() # define UNIQUE(wpwpw) (wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end()) # define LOWER(epepe) transform(ALL((epepe)),(epepe).begin(),TL<char>) # define UPPER(rprpr) transform(ALL((rprpr)),(rprpr).begin(),TU<char>) # define FOR(i,tptpt,ypypy) for(LL i=(tptpt);i<(ypypy);i++) # define REP(i,upupu) FOR(i,0,upupu) # define INIT std::ios::sync_with_stdio(false);std::cin.tie(0) # pragma warning(disable:4996) int h, w; struct st { char c[11][11]; int cnt; }; void dfs(st &s, int i, int j, char before,char after) { s.c[i][j] = after; REP(k, 4) { int ni = i + dy[k], nj = j + dx[k]; if (0 <= ni&&ni < h && 0 <= nj&&nj < w&&s.c[ni][nj] == before) { dfs(s, ni, nj, before, after); } } } bool isok(st &s) { REP(i, h)REP(j, w) { if (s.c[i][j] != s.c[0][0])return false; } return true; } char dir[] = "RGB"; void solve() { st s; s.cnt = 0; REP(i, h)REP(j, w)cin >> s.c[i][j]; queue<st>que; que.push(s); while (que.size()) { st q = que.front(); que.pop(); if (isok(q)) { cout << q.cnt << endl; return; } st u; REP(i, 3) { u = q; if (dir[i] == u.c[0][0])continue; dfs(u, 0, 0, u.c[0][0], dir[i]); u.cnt++; que.push(u); } } } int main() { while (cin >> w >> h&&w&&h) { solve(); } } ```
#include<iostream> #include<queue> using namespace std; int x,y; struct grid{ char g[11][11]; int count; }; void DFS(grid& gr,char firstcell,char changecolor,int posx,int posy){ //if(posx < 0 || posx >= x || posy < 0 || posy >= y || gr.g[posx][posy] != firstcell){ if(gr.g[posx][posy] != firstcell){ return; } gr.g[posx][posy] = changecolor; DFS(gr,firstcell,changecolor,posx+1,posy); DFS(gr,firstcell,changecolor,posx-1,posy); DFS(gr,firstcell,changecolor,posx,posy+1); DFS(gr,firstcell,changecolor,posx,posy-1); } int main(){ while(1){ cin >> x >> y; if(x==0 && y==0){ break; } grid start; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ cin >> start.g[j][i]; } } char firstcell = start.g[0][0]; bool flag = true; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ if(start.g[j][i]!=firstcell){ flag = false; break; } } if(!flag){ break; } } if(flag){ cout << 0 << endl; continue; } start.count = 0; queue<grid> q; q.push(start); while(1){ grid BeforeGrid = q.front(); q.pop(); BeforeGrid.count++; grid tmp = BeforeGrid; firstcell = tmp.g[0][0]; if(firstcell != 'R'){ DFS(tmp,firstcell,'R',0,0); flag = true; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ if(tmp.g[j][i]!='R'){ flag = false; break; } } if(!flag){ break; } } if(flag){ cout << tmp.count << endl; break; } q.push(tmp); tmp = BeforeGrid; } if(firstcell != 'G'){ DFS(tmp,firstcell,'G',0,0); flag = true; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ if(tmp.g[j][i]!='G'){ flag = false; break; } } if(!flag){ break; } } if(flag){ cout << tmp.count << endl; break; } q.push(tmp); tmp = BeforeGrid; } if(firstcell != 'B'){ DFS(tmp,firstcell,'B',0,0); flag = true; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ if(tmp.g[j][i]!='B'){ flag = false; break; } } if(!flag){ break; } } if(flag){ cout << tmp.count << endl; break; } q.push(tmp); tmp = BeforeGrid; } } } return 0; }
### Prompt Please create a solution in Cpp to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include<iostream> #include<queue> using namespace std; int x,y; struct grid{ char g[11][11]; int count; }; void DFS(grid& gr,char firstcell,char changecolor,int posx,int posy){ //if(posx < 0 || posx >= x || posy < 0 || posy >= y || gr.g[posx][posy] != firstcell){ if(gr.g[posx][posy] != firstcell){ return; } gr.g[posx][posy] = changecolor; DFS(gr,firstcell,changecolor,posx+1,posy); DFS(gr,firstcell,changecolor,posx-1,posy); DFS(gr,firstcell,changecolor,posx,posy+1); DFS(gr,firstcell,changecolor,posx,posy-1); } int main(){ while(1){ cin >> x >> y; if(x==0 && y==0){ break; } grid start; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ cin >> start.g[j][i]; } } char firstcell = start.g[0][0]; bool flag = true; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ if(start.g[j][i]!=firstcell){ flag = false; break; } } if(!flag){ break; } } if(flag){ cout << 0 << endl; continue; } start.count = 0; queue<grid> q; q.push(start); while(1){ grid BeforeGrid = q.front(); q.pop(); BeforeGrid.count++; grid tmp = BeforeGrid; firstcell = tmp.g[0][0]; if(firstcell != 'R'){ DFS(tmp,firstcell,'R',0,0); flag = true; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ if(tmp.g[j][i]!='R'){ flag = false; break; } } if(!flag){ break; } } if(flag){ cout << tmp.count << endl; break; } q.push(tmp); tmp = BeforeGrid; } if(firstcell != 'G'){ DFS(tmp,firstcell,'G',0,0); flag = true; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ if(tmp.g[j][i]!='G'){ flag = false; break; } } if(!flag){ break; } } if(flag){ cout << tmp.count << endl; break; } q.push(tmp); tmp = BeforeGrid; } if(firstcell != 'B'){ DFS(tmp,firstcell,'B',0,0); flag = true; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ if(tmp.g[j][i]!='B'){ flag = false; break; } } if(!flag){ break; } } if(flag){ cout << tmp.count << endl; break; } q.push(tmp); tmp = BeforeGrid; } } } return 0; } ```
// 2014/09/12 Tazoe #include <iostream> #include <queue> #include <string> #include <map> using namespace std; struct state{ string G; int N; }; bool is_over(string G) { bool flg = true; for(int i=1; i<G.size(); i++){ if(G[i]!=G[0]){ flg = false; break; } } return flg; } void DFS(char g[12][12], char F, char T, int x, int y) { if(g[y][x]!=F) return; g[y][x] = T; DFS(g, F, T, x-1, y); DFS(g, F, T, x+1, y); DFS(g, F, T, x, y-1); DFS(g, F, T, x, y+1); } string fill(string G, char C, int X, int Y) { char g[12][12]; for(int y=0; y<Y+2; y++){ for(int x=0; x<X+2; x++){ g[y][x] = 'N'; } } // 文字列から2次元配列へ for(int y=0; y<Y; y++){ for(int x=0; x<X; x++){ g[y+1][x+1] = G[y*X+x]; } } DFS(g, G[0], C, 1, 1); // 2次元配列から文字列へ for(int y=0; y<Y; y++){ for(int x=0; x<X; x++){ G[y*X+x] = g[y+1][x+1]; } } return G; } int main() { while(true){ int X, Y; cin >> X >> Y; if(X==0 && Y==0) break; string G = ""; for(int y=0; y<Y; y++){ for(int x=0; x<X; x++){ string c; cin >> c; G += c; } } queue<struct state> que; map<string, bool> memo; struct state now; now.G = G; now.N = 0; que.push(now); memo[now.G] = true; while(!que.empty()){ now = que.front(); que.pop(); if(is_over(now.G)){ cout << now.N << endl; break; } struct state nex1, nex2; if(now.G[0]=='R'){ nex1.G = fill(now.G, 'G', X, Y); nex2.G = fill(now.G, 'B', X, Y); nex1.N = nex2.N = now.N+1; } else if(now.G[0]=='G'){ nex1.G = fill(now.G, 'R', X, Y); nex2.G = fill(now.G, 'B', X, Y); nex1.N = nex2.N = now.N+1; } else if(now.G[0]=='B'){ nex1.G = fill(now.G, 'R', X, Y); nex2.G = fill(now.G, 'G', X, Y); nex1.N = nex2.N = now.N+1; } if(memo.find(nex1.G)==memo.end()){ que.push(nex1); memo[nex1.G] = true; } if(memo.find(nex2.G)==memo.end()){ que.push(nex2); memo[nex2.G] = true; } } } return 0; }
### Prompt Develop a solution in cpp to the problem described below: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp // 2014/09/12 Tazoe #include <iostream> #include <queue> #include <string> #include <map> using namespace std; struct state{ string G; int N; }; bool is_over(string G) { bool flg = true; for(int i=1; i<G.size(); i++){ if(G[i]!=G[0]){ flg = false; break; } } return flg; } void DFS(char g[12][12], char F, char T, int x, int y) { if(g[y][x]!=F) return; g[y][x] = T; DFS(g, F, T, x-1, y); DFS(g, F, T, x+1, y); DFS(g, F, T, x, y-1); DFS(g, F, T, x, y+1); } string fill(string G, char C, int X, int Y) { char g[12][12]; for(int y=0; y<Y+2; y++){ for(int x=0; x<X+2; x++){ g[y][x] = 'N'; } } // 文字列から2次元配列へ for(int y=0; y<Y; y++){ for(int x=0; x<X; x++){ g[y+1][x+1] = G[y*X+x]; } } DFS(g, G[0], C, 1, 1); // 2次元配列から文字列へ for(int y=0; y<Y; y++){ for(int x=0; x<X; x++){ G[y*X+x] = g[y+1][x+1]; } } return G; } int main() { while(true){ int X, Y; cin >> X >> Y; if(X==0 && Y==0) break; string G = ""; for(int y=0; y<Y; y++){ for(int x=0; x<X; x++){ string c; cin >> c; G += c; } } queue<struct state> que; map<string, bool> memo; struct state now; now.G = G; now.N = 0; que.push(now); memo[now.G] = true; while(!que.empty()){ now = que.front(); que.pop(); if(is_over(now.G)){ cout << now.N << endl; break; } struct state nex1, nex2; if(now.G[0]=='R'){ nex1.G = fill(now.G, 'G', X, Y); nex2.G = fill(now.G, 'B', X, Y); nex1.N = nex2.N = now.N+1; } else if(now.G[0]=='G'){ nex1.G = fill(now.G, 'R', X, Y); nex2.G = fill(now.G, 'B', X, Y); nex1.N = nex2.N = now.N+1; } else if(now.G[0]=='B'){ nex1.G = fill(now.G, 'R', X, Y); nex2.G = fill(now.G, 'G', X, Y); nex1.N = nex2.N = now.N+1; } if(memo.find(nex1.G)==memo.end()){ que.push(nex1); memo[nex1.G] = true; } if(memo.find(nex2.G)==memo.end()){ que.push(nex2); memo[nex2.G] = true; } } } return 0; } ```
#include<iostream> #include<queue> using namespace std; struct grid{ char g[12][12]; int cost; }; void paint(grid& gr,char l_u_cell,char changecolor,int p_x,int p_y){ if(gr.g[p_y][p_x]!=l_u_cell){ return ; } gr.g[p_y][p_x]=changecolor; paint(gr,l_u_cell,changecolor,p_x+1,p_y); paint(gr,l_u_cell,changecolor,p_x-1,p_y); paint(gr,l_u_cell,changecolor,p_x,p_y+1); paint(gr,l_u_cell,changecolor,p_x,p_y-1); } bool chacker(grid& gr,char l_u_cell,int X,int Y){ bool flag=true; for(int i=1;i<=Y;i++){ for(int j=1;j<=X;j++){ if(l_u_cell!=gr.g[i][j]){flag=false;break;} } } return flag; } int main(){ while(1){ int X,Y; cin>>X>>Y; if(X==0&&Y==0)break; grid first; for(int i=1;i<=Y;i++){ for(int j=1;j<=X;j++){ cin>>first.g[i][j]; } } first.cost=0; queue<grid> que; que.push(first); while(1){ grid Grid=que.front();que.pop(); char l_u_cell; l_u_cell=Grid.g[1][1]; if(chacker(Grid,l_u_cell,X,Y)){ cout<<Grid.cost<<endl; break; } if(l_u_cell != 'R'){ grid tmp=Grid; tmp.cost=tmp.cost+1; paint(tmp,l_u_cell,'R',1,1); que.push(tmp); } if(l_u_cell != 'G'){ grid tmp=Grid; tmp.cost=tmp.cost+1; paint(tmp,l_u_cell,'G',1,1); que.push(tmp); } if(l_u_cell != 'B'){ grid tmp=Grid; tmp.cost=tmp.cost+1; paint(tmp,l_u_cell,'B',1,1); que.push(tmp); } } } return 0; }
### Prompt In Cpp, your task is to solve the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include<iostream> #include<queue> using namespace std; struct grid{ char g[12][12]; int cost; }; void paint(grid& gr,char l_u_cell,char changecolor,int p_x,int p_y){ if(gr.g[p_y][p_x]!=l_u_cell){ return ; } gr.g[p_y][p_x]=changecolor; paint(gr,l_u_cell,changecolor,p_x+1,p_y); paint(gr,l_u_cell,changecolor,p_x-1,p_y); paint(gr,l_u_cell,changecolor,p_x,p_y+1); paint(gr,l_u_cell,changecolor,p_x,p_y-1); } bool chacker(grid& gr,char l_u_cell,int X,int Y){ bool flag=true; for(int i=1;i<=Y;i++){ for(int j=1;j<=X;j++){ if(l_u_cell!=gr.g[i][j]){flag=false;break;} } } return flag; } int main(){ while(1){ int X,Y; cin>>X>>Y; if(X==0&&Y==0)break; grid first; for(int i=1;i<=Y;i++){ for(int j=1;j<=X;j++){ cin>>first.g[i][j]; } } first.cost=0; queue<grid> que; que.push(first); while(1){ grid Grid=que.front();que.pop(); char l_u_cell; l_u_cell=Grid.g[1][1]; if(chacker(Grid,l_u_cell,X,Y)){ cout<<Grid.cost<<endl; break; } if(l_u_cell != 'R'){ grid tmp=Grid; tmp.cost=tmp.cost+1; paint(tmp,l_u_cell,'R',1,1); que.push(tmp); } if(l_u_cell != 'G'){ grid tmp=Grid; tmp.cost=tmp.cost+1; paint(tmp,l_u_cell,'G',1,1); que.push(tmp); } if(l_u_cell != 'B'){ grid tmp=Grid; tmp.cost=tmp.cost+1; paint(tmp,l_u_cell,'B',1,1); que.push(tmp); } } } return 0; } ```
#define _USE_MATH_DEFINES #define INF 0x3f3f3f3f #include <cstdio> #include <iostream> #include <sstream> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <limits> #include <map> #include <string> #include <cstring> #include <set> #include <deque> #include <bitset> #include <list> #include <cctype> #include <utility> using namespace std; typedef long long ll; typedef pair <int,int> P; typedef pair <int,P> PP; int tx[] = {0,1,0,-1}; int ty[] = {-1,0,1,0}; static const double EPS = 1e-8; static const char RGB[]={'R','G','B'}; int w,h; class Data{ public: char stage[10][10]; int count; Data() : count(0), stage(){} Data(char _stage[10][10],int _count){ memcpy(this->stage,_stage,sizeof(char)*10*10); this->count = _count; } bool operator>(const Data& d) const{ return this->count > d.count; } bool operator<(const Data& d) const{ return this->count < d.count; } }; void dfs(char stage[10][10],int x,int y,int from,int to,bool visited[10][10]){ stage[y][x] = to; visited[y][x] = true; for(int i=0;i<4;i++){ int dx = x + tx[i]; int dy = y + ty[i]; if(dx < 0 || dx >= w || dy < 0 || dy >= h) continue; if(stage[dy][dx] != from ) continue; if(visited[dy][dx]) continue; dfs(stage,dx,dy,from,to,visited); } } string stg2str(char stage[10][10]){ string res = ""; for(int y=0;y<h;y++){ for(int x=0;x<w;x++){ res += stage[y][x]; } } return res; } int main() { while(~scanf("%d %d",&w,&h)){ if(h==w && w==0) break; char stage[10][10]; memset(stage,'\0',sizeof(stage)); for(int y=0;y<h;y++){ for(int x=0;x<w;x++){ char ch; cin >> ch; stage[y][x] = ch; } } int minv = numeric_limits<int>::max(); string init = stg2str(stage); priority_queue<Data,vector<Data>,greater<Data> > que; que.push(Data(stage,0)); map<string,int> cost; cost[init] = 0; while(!que.empty()){ Data d = que.top(); que.pop(); for(int i=0;i<3;i++){ char next[10][10]; memcpy(next,d.stage,sizeof(char)*10*10); bool visited[10][10]; memset(visited,0,sizeof(visited)); dfs(next,0,0,d.stage[0][0],RGB[i],visited); string str = stg2str(next); if(cost.find(str) != cost.end() && cost[str] <= d.count + 1) continue; cost[str] = d.count + 1; que.push(Data(next,d.count+1)); } } for(int i=0;i<3;i++){ string ans=""; while(ans.size() < w*h) ans.push_back(RGB[i]); if(cost.find(ans) != cost.end()) minv = min(cost[ans],minv); } printf("%d\n",minv); } }
### Prompt Please create a solution in cpp to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #define _USE_MATH_DEFINES #define INF 0x3f3f3f3f #include <cstdio> #include <iostream> #include <sstream> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <limits> #include <map> #include <string> #include <cstring> #include <set> #include <deque> #include <bitset> #include <list> #include <cctype> #include <utility> using namespace std; typedef long long ll; typedef pair <int,int> P; typedef pair <int,P> PP; int tx[] = {0,1,0,-1}; int ty[] = {-1,0,1,0}; static const double EPS = 1e-8; static const char RGB[]={'R','G','B'}; int w,h; class Data{ public: char stage[10][10]; int count; Data() : count(0), stage(){} Data(char _stage[10][10],int _count){ memcpy(this->stage,_stage,sizeof(char)*10*10); this->count = _count; } bool operator>(const Data& d) const{ return this->count > d.count; } bool operator<(const Data& d) const{ return this->count < d.count; } }; void dfs(char stage[10][10],int x,int y,int from,int to,bool visited[10][10]){ stage[y][x] = to; visited[y][x] = true; for(int i=0;i<4;i++){ int dx = x + tx[i]; int dy = y + ty[i]; if(dx < 0 || dx >= w || dy < 0 || dy >= h) continue; if(stage[dy][dx] != from ) continue; if(visited[dy][dx]) continue; dfs(stage,dx,dy,from,to,visited); } } string stg2str(char stage[10][10]){ string res = ""; for(int y=0;y<h;y++){ for(int x=0;x<w;x++){ res += stage[y][x]; } } return res; } int main() { while(~scanf("%d %d",&w,&h)){ if(h==w && w==0) break; char stage[10][10]; memset(stage,'\0',sizeof(stage)); for(int y=0;y<h;y++){ for(int x=0;x<w;x++){ char ch; cin >> ch; stage[y][x] = ch; } } int minv = numeric_limits<int>::max(); string init = stg2str(stage); priority_queue<Data,vector<Data>,greater<Data> > que; que.push(Data(stage,0)); map<string,int> cost; cost[init] = 0; while(!que.empty()){ Data d = que.top(); que.pop(); for(int i=0;i<3;i++){ char next[10][10]; memcpy(next,d.stage,sizeof(char)*10*10); bool visited[10][10]; memset(visited,0,sizeof(visited)); dfs(next,0,0,d.stage[0][0],RGB[i],visited); string str = stg2str(next); if(cost.find(str) != cost.end() && cost[str] <= d.count + 1) continue; cost[str] = d.count + 1; que.push(Data(next,d.count+1)); } } for(int i=0;i<3;i++){ string ans=""; while(ans.size() < w*h) ans.push_back(RGB[i]); if(cost.find(ans) != cost.end()) minv = min(cost[ans],minv); } printf("%d\n",minv); } } ```
#include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() #define fi first #define se second template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; } template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; } typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<int, pii> pip; const ll INF = 1ll<<29; const ll MOD = 1000000007; const double EPS = 1e-9; int X, Y; char maps[10][10]; int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; int ans; struct Data { int cnt; char m[10][10]; Data(int _cnt, char _m[10][10]) { cnt = _cnt; REP(i, 10) REP(j, 10) m[i][j] = _m[i][j]; } }; int main() { while (cin >> X >> Y, X || Y) { REP(i, Y) REP(j, X) { string s; cin >> s; int num; switch (s[0]) { case 'R': num = 0; break; case 'G': num = 1; break; case 'B': num = 2; break; } maps[i][j] = num; } queue<Data> que; que.push(Data(0, maps)); int ans = INF; while (!que.empty()) { Data now = que.front(); que.pop(); if (ans <= now.cnt) continue; bool en = true; FOR(i, 1, Y) REP(j, X) if (now.m[i][j] != now.m[i - 1][j]) en = false; FOR(j, 1, X) REP(i, Y) if (now.m[i][j] != now.m[i][j - 1]) en = false; if (en) { ans = now.cnt; continue; } queue<pii> que2; bool done[10][10] = {}; vector<pii> v[3]; que2.push(pii(0, 0)); done[0][0] = true; v[now.m[0][0]].push_back(pii(0, 0)); char base = now.m[0][0]; while (!que2.empty()) { int x = que2.front().se, y = que2.front().fi; que2.pop(); REP(i, 4) { int nx = x + dx[i], ny = y + dy[i]; if (!(nx >= 0 && nx < X && ny >= 0 && ny < Y)) continue; if (done[ny][nx]) continue; if (now.m[ny][nx] == base) que2.push(pii(ny, nx)); done[ny][nx] = true; if (now.m[ny][nx] == base || (base != now.m[ny][nx] && v[now.m[ny][nx]].empty())) v[now.m[ny][nx]].push_back(pii(ny, nx)); } } REP(i, 3) if (i != base) { if (v[i].empty()) continue; REP(j, v[base].size()) now.m[v[base][j].fi][v[base][j].se] = i; que.push(Data(now.cnt + 1, now.m)); } } cout << ans << endl; } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() #define fi first #define se second template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; } template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; } typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<int, pii> pip; const ll INF = 1ll<<29; const ll MOD = 1000000007; const double EPS = 1e-9; int X, Y; char maps[10][10]; int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; int ans; struct Data { int cnt; char m[10][10]; Data(int _cnt, char _m[10][10]) { cnt = _cnt; REP(i, 10) REP(j, 10) m[i][j] = _m[i][j]; } }; int main() { while (cin >> X >> Y, X || Y) { REP(i, Y) REP(j, X) { string s; cin >> s; int num; switch (s[0]) { case 'R': num = 0; break; case 'G': num = 1; break; case 'B': num = 2; break; } maps[i][j] = num; } queue<Data> que; que.push(Data(0, maps)); int ans = INF; while (!que.empty()) { Data now = que.front(); que.pop(); if (ans <= now.cnt) continue; bool en = true; FOR(i, 1, Y) REP(j, X) if (now.m[i][j] != now.m[i - 1][j]) en = false; FOR(j, 1, X) REP(i, Y) if (now.m[i][j] != now.m[i][j - 1]) en = false; if (en) { ans = now.cnt; continue; } queue<pii> que2; bool done[10][10] = {}; vector<pii> v[3]; que2.push(pii(0, 0)); done[0][0] = true; v[now.m[0][0]].push_back(pii(0, 0)); char base = now.m[0][0]; while (!que2.empty()) { int x = que2.front().se, y = que2.front().fi; que2.pop(); REP(i, 4) { int nx = x + dx[i], ny = y + dy[i]; if (!(nx >= 0 && nx < X && ny >= 0 && ny < Y)) continue; if (done[ny][nx]) continue; if (now.m[ny][nx] == base) que2.push(pii(ny, nx)); done[ny][nx] = true; if (now.m[ny][nx] == base || (base != now.m[ny][nx] && v[now.m[ny][nx]].empty())) v[now.m[ny][nx]].push_back(pii(ny, nx)); } } REP(i, 3) if (i != base) { if (v[i].empty()) continue; REP(j, v[base].size()) now.m[v[base][j].fi][v[base][j].se] = i; que.push(Data(now.cnt + 1, now.m)); } } cout << ans << endl; } return 0; } ```
#include<iostream> #include<queue> using namespace std; struct grid{ char g[12][12]; int cnt; }; void fill(grid &gr,char C,char c,int x,int y){ if(gr.g[y][x] != C){ return; } gr.g[y][x] = c; fill(gr,C,c,x+1,y); fill(gr,C,c,x-1,y); fill(gr,C,c,x,y+1); fill(gr,C,c,x,y-1); } bool check(grid &gr,char C,int x,int y){ bool flag = true; for(int i=1;i<=y;i++){ for(int j=1;j<=x;j++){ if(C != gr.g[i][j]){ flag = false; break; } } } return flag; } int main(){ while(1){ int x,y; cin >> x >> y; if(x==0 && y==0) break; grid st; for(int i=1;i<=y;i++){ for(int j=1;j<=x;j++){ cin >> st.g[i][j]; } } st.cnt = 0; queue<grid> q; q.push(st); while(1){ grid data = q.front(); q.pop(); char C; C = data.g[1][1]; if(check(data,C,x,y)){ cout << data.cnt << endl; break; } if(C != 'R'){ grid tmp = data; tmp.cnt = tmp.cnt+1; fill(tmp,C,'R',1,1); q.push(tmp); } if(C != 'G'){ grid tmp = data; tmp.cnt = tmp.cnt+1; fill(tmp,C,'G',1,1); q.push(tmp); } if(C != 'B'){ grid tmp = data; tmp.cnt = tmp.cnt+1; fill(tmp,C,'B',1,1); q.push(tmp); } } } return 0; }
### Prompt In CPP, your task is to solve the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include<iostream> #include<queue> using namespace std; struct grid{ char g[12][12]; int cnt; }; void fill(grid &gr,char C,char c,int x,int y){ if(gr.g[y][x] != C){ return; } gr.g[y][x] = c; fill(gr,C,c,x+1,y); fill(gr,C,c,x-1,y); fill(gr,C,c,x,y+1); fill(gr,C,c,x,y-1); } bool check(grid &gr,char C,int x,int y){ bool flag = true; for(int i=1;i<=y;i++){ for(int j=1;j<=x;j++){ if(C != gr.g[i][j]){ flag = false; break; } } } return flag; } int main(){ while(1){ int x,y; cin >> x >> y; if(x==0 && y==0) break; grid st; for(int i=1;i<=y;i++){ for(int j=1;j<=x;j++){ cin >> st.g[i][j]; } } st.cnt = 0; queue<grid> q; q.push(st); while(1){ grid data = q.front(); q.pop(); char C; C = data.g[1][1]; if(check(data,C,x,y)){ cout << data.cnt << endl; break; } if(C != 'R'){ grid tmp = data; tmp.cnt = tmp.cnt+1; fill(tmp,C,'R',1,1); q.push(tmp); } if(C != 'G'){ grid tmp = data; tmp.cnt = tmp.cnt+1; fill(tmp,C,'G',1,1); q.push(tmp); } if(C != 'B'){ grid tmp = data; tmp.cnt = tmp.cnt+1; fill(tmp,C,'B',1,1); q.push(tmp); } } } return 0; } ```
#include <bits/stdc++.h> using namespace std; string co="RGB"; int W,H; typedef struct{ int cnt,area; char f[12][12]; }F; int chk(F tmp){ for(int i=1;i<=H;i++){ for(int j=1;j<=W;j++){ if(tmp.f[1][1]!=tmp.f[j][i])return 0; } } return 1; } F dt,df,dc; int dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; typedef pair<int,int>P; void flgon(){ queue<P>Q; Q.push(P(1,1)); while(!Q.empty()){ P p=Q.front();Q.pop(); if(df.f[p.first][p.second]=='#'){ df.f[p.first][p.second]='*'; for(int i=0;i<4;i++){ if(df.f[p.first+dx[i]][p.second+dy[i]]=='#' && dt.f[p.first+dx[i]][p.second+dy[i]]==dt.f[p.first][p.second]){ Q.push(P(p.first+dx[i],p.second+dy[i])); } } } } } int flgcnt(){ int cnt=0; for(int i=1;i<=H;i++){ for(int j=1;j<=W;j++){ if(df.f[j][i]=='*')cnt++; } } return cnt; } void flgdraw(char c){ for(int i=1;i<=H;i++){ for(int j=1;j<=W;j++){ if(df.f[j][i]=='*')dt.f[j][i]=c; } } } int main(){ for(int i=0;i<144;i++)dc.f[i/12][i%12]='#'; dc.cnt=0; while(cin>>W>>H,W){ F tmp=dc; for(int i=1;i<=H;i++){ for(int j=1;j<=W;j++){ cin>>tmp.f[j][i]; } } dt=tmp; df=dc;flgon(); tmp.area=flgcnt(); queue<F>Q; Q.push(tmp); while(!Q.empty()){ tmp=Q.front(); Q.pop(); if(chk(tmp)){ cout<<tmp.cnt<<endl; break; } for(int i=0;i<3;i++){ if(tmp.f[1][1]==co[i])continue; dt=tmp; df=dc;flgon(); flgdraw(co[i]); df=dc;flgon(); int k=flgcnt(); if(tmp.area==k)continue; dt.cnt=tmp.cnt+1; dt.area=k; Q.push(dt); } } } return 0; }
### Prompt In cpp, your task is to solve the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <bits/stdc++.h> using namespace std; string co="RGB"; int W,H; typedef struct{ int cnt,area; char f[12][12]; }F; int chk(F tmp){ for(int i=1;i<=H;i++){ for(int j=1;j<=W;j++){ if(tmp.f[1][1]!=tmp.f[j][i])return 0; } } return 1; } F dt,df,dc; int dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; typedef pair<int,int>P; void flgon(){ queue<P>Q; Q.push(P(1,1)); while(!Q.empty()){ P p=Q.front();Q.pop(); if(df.f[p.first][p.second]=='#'){ df.f[p.first][p.second]='*'; for(int i=0;i<4;i++){ if(df.f[p.first+dx[i]][p.second+dy[i]]=='#' && dt.f[p.first+dx[i]][p.second+dy[i]]==dt.f[p.first][p.second]){ Q.push(P(p.first+dx[i],p.second+dy[i])); } } } } } int flgcnt(){ int cnt=0; for(int i=1;i<=H;i++){ for(int j=1;j<=W;j++){ if(df.f[j][i]=='*')cnt++; } } return cnt; } void flgdraw(char c){ for(int i=1;i<=H;i++){ for(int j=1;j<=W;j++){ if(df.f[j][i]=='*')dt.f[j][i]=c; } } } int main(){ for(int i=0;i<144;i++)dc.f[i/12][i%12]='#'; dc.cnt=0; while(cin>>W>>H,W){ F tmp=dc; for(int i=1;i<=H;i++){ for(int j=1;j<=W;j++){ cin>>tmp.f[j][i]; } } dt=tmp; df=dc;flgon(); tmp.area=flgcnt(); queue<F>Q; Q.push(tmp); while(!Q.empty()){ tmp=Q.front(); Q.pop(); if(chk(tmp)){ cout<<tmp.cnt<<endl; break; } for(int i=0;i<3;i++){ if(tmp.f[1][1]==co[i])continue; dt=tmp; df=dc;flgon(); flgdraw(co[i]); df=dc;flgon(); int k=flgcnt(); if(tmp.area==k)continue; dt.cnt=tmp.cnt+1; dt.area=k; Q.push(dt); } } } return 0; } ```
#include<bits/stdc++.h> using namespace std; const int dx[]={-1,0,1,0}; const int dy[]={0,-1,0,1}; int H,W; int fld[10][10]; int lim; void change(int y,int x,int c,int f){ fld[y][x]=c; for(int d=0;d<4;d++){ int ny=y+dy[d],nx=x+dx[d]; if(ny<0||ny>=H||nx<0||nx>=W||fld[ny][nx]!=f)continue; change(ny,nx,c,f); } } bool dfs(int t=0){ bool ok=true; for(int i=0;i<H;i++)for(int j=0;j<W;j++){ if(fld[0][0]!=fld[i][j])ok=false; } if(ok)return true; if(t==lim)return false; int _fld[10][10]; for(int i=0;i<H;i++)for(int j=0;j<W;j++){ _fld[i][j]=fld[i][j]; } bool ret=false; for(int k=0;k<3;k++)if(k!=fld[0][0]){ change(0,0,k,fld[0][0]); ret|=dfs(t+1); for(int i=0;i<H;i++)for(int j=0;j<W;j++){ fld[i][j]=_fld[i][j]; } } return ret; } int main(){ while(cin>>W>>H,W||H){ for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ char c; cin>>c; switch(c){ case 'R':fld[i][j]=0;break; case 'G':fld[i][j]=1;break; case 'B':fld[i][j]=2;break; } } } for(lim=0;;lim++)if(dfs())break; cout<<lim<<endl; } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include<bits/stdc++.h> using namespace std; const int dx[]={-1,0,1,0}; const int dy[]={0,-1,0,1}; int H,W; int fld[10][10]; int lim; void change(int y,int x,int c,int f){ fld[y][x]=c; for(int d=0;d<4;d++){ int ny=y+dy[d],nx=x+dx[d]; if(ny<0||ny>=H||nx<0||nx>=W||fld[ny][nx]!=f)continue; change(ny,nx,c,f); } } bool dfs(int t=0){ bool ok=true; for(int i=0;i<H;i++)for(int j=0;j<W;j++){ if(fld[0][0]!=fld[i][j])ok=false; } if(ok)return true; if(t==lim)return false; int _fld[10][10]; for(int i=0;i<H;i++)for(int j=0;j<W;j++){ _fld[i][j]=fld[i][j]; } bool ret=false; for(int k=0;k<3;k++)if(k!=fld[0][0]){ change(0,0,k,fld[0][0]); ret|=dfs(t+1); for(int i=0;i<H;i++)for(int j=0;j<W;j++){ fld[i][j]=_fld[i][j]; } } return ret; } int main(){ while(cin>>W>>H,W||H){ for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ char c; cin>>c; switch(c){ case 'R':fld[i][j]=0;break; case 'G':fld[i][j]=1;break; case 'B':fld[i][j]=2;break; } } } for(lim=0;;lim++)if(dfs())break; cout<<lim<<endl; } return 0; } ```
#include<iostream> #include<queue> using namespace std; struct grid{ char g[12][12]; int turn; }; void fill(grid &gr,char cell,char color,int fx,int fy){ if(gr.g[fy][fx] != cell){ return; } gr.g[fy][fx] = color; fill(gr,cell,color,fx+1,fy); fill(gr,cell,color,fx-1,fy); fill(gr,cell,color,fx,fy+1); fill(gr,cell,color,fx,fy-1); } bool check(grid &gr,char cell,int x,int y){ bool flag = 1; for(int i=1;i<=y;i++){ for(int j=1;j<=x;j++){ if(cell != gr.g[i][j]){ flag = 0; break; } } } return flag; } int main(){ while(1){ int x,y; cin >> x >> y; if(x==0 && y==0) break; grid first; for(int i=1;i<=y;i++){ for(int j=1;j<=x;j++){ cin >> first.g[i][j]; } } first.turn = 0; queue<grid> q; q.push(first); while(1){ grid G = q.front(); q.pop(); char cell; cell = G.g[1][1]; if(check(G,cell,x,y)){ cout << G.turn << endl; break; } if(cell != 'R'){ grid tmp = G; tmp.turn = tmp.turn+1; fill(tmp,cell,'R',1,1); q.push(tmp); } if(cell != 'G'){ grid tmp = G; tmp.turn = tmp.turn+1; fill(tmp,cell,'G',1,1); q.push(tmp); } if(cell != 'B'){ grid tmp = G; tmp.turn = tmp.turn+1; fill(tmp,cell,'B',1,1); q.push(tmp); } } } return 0; }
### Prompt Please create a solution in Cpp to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include<iostream> #include<queue> using namespace std; struct grid{ char g[12][12]; int turn; }; void fill(grid &gr,char cell,char color,int fx,int fy){ if(gr.g[fy][fx] != cell){ return; } gr.g[fy][fx] = color; fill(gr,cell,color,fx+1,fy); fill(gr,cell,color,fx-1,fy); fill(gr,cell,color,fx,fy+1); fill(gr,cell,color,fx,fy-1); } bool check(grid &gr,char cell,int x,int y){ bool flag = 1; for(int i=1;i<=y;i++){ for(int j=1;j<=x;j++){ if(cell != gr.g[i][j]){ flag = 0; break; } } } return flag; } int main(){ while(1){ int x,y; cin >> x >> y; if(x==0 && y==0) break; grid first; for(int i=1;i<=y;i++){ for(int j=1;j<=x;j++){ cin >> first.g[i][j]; } } first.turn = 0; queue<grid> q; q.push(first); while(1){ grid G = q.front(); q.pop(); char cell; cell = G.g[1][1]; if(check(G,cell,x,y)){ cout << G.turn << endl; break; } if(cell != 'R'){ grid tmp = G; tmp.turn = tmp.turn+1; fill(tmp,cell,'R',1,1); q.push(tmp); } if(cell != 'G'){ grid tmp = G; tmp.turn = tmp.turn+1; fill(tmp,cell,'G',1,1); q.push(tmp); } if(cell != 'B'){ grid tmp = G; tmp.turn = tmp.turn+1; fill(tmp,cell,'B',1,1); q.push(tmp); } } } return 0; } ```
#include <iostream> #include <algorithm> #include <utility> #include <queue> #include <cstring> using namespace std; typedef pair<int, int> P; struct State { int turn; char board[10][10]; }; int main() { while(true) { int X, Y; cin >> X >> Y; if(X == 0 && Y == 0) break; char board[10][10]; for(int i = 0; i < Y; i++) { for(int j = 0; j < X; j++) { char c; cin >> c; if(c == 'R') board[i][j] = 0; if(c == 'G') board[i][j] = 1; if(c == 'B') board[i][j] = 2; } } State S; S.turn = 0; memcpy(S.board, board, sizeof(board)); queue<State> Q; Q.push(S); int res; while(!Q.empty()) { State s = Q.front(); Q.pop(); bool ok = true; for(int i = 0; i < Y; i++) { for(int j = 0; j < X; j++) { if(s.board[i][j] != s.board[0][0]) { ok = false; break; } } } if(ok) { res = s.turn; break; } for(char c = 0; c < 3; c++) { if(s.board[0][0] == c) continue; memcpy(S.board, s.board, sizeof(s.board)); queue<P> Q2; Q2.emplace(0, 0); char prev = S.board[0][0]; S.board[0][0] = c; while(!Q2.empty()) { P p = Q2.front(); Q2.pop(); for(int i = 0; i < 4; i++) { const int dy[4] = { 0, -1, 0, 1 }; const int dx[4] = { 1, 0, -1, 0 }; int ny = p.first + dy[i]; int nx = p.second + dx[i]; if(0 <= ny && ny < Y && 0 <= nx && nx < X && S.board[ny][nx] == prev) { S.board[ny][nx] = c; Q2.emplace(ny, nx); } } } S.turn = s.turn + 1; Q.push(S); } } cout << res << endl; } }
### Prompt Please formulate a cpp solution to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <iostream> #include <algorithm> #include <utility> #include <queue> #include <cstring> using namespace std; typedef pair<int, int> P; struct State { int turn; char board[10][10]; }; int main() { while(true) { int X, Y; cin >> X >> Y; if(X == 0 && Y == 0) break; char board[10][10]; for(int i = 0; i < Y; i++) { for(int j = 0; j < X; j++) { char c; cin >> c; if(c == 'R') board[i][j] = 0; if(c == 'G') board[i][j] = 1; if(c == 'B') board[i][j] = 2; } } State S; S.turn = 0; memcpy(S.board, board, sizeof(board)); queue<State> Q; Q.push(S); int res; while(!Q.empty()) { State s = Q.front(); Q.pop(); bool ok = true; for(int i = 0; i < Y; i++) { for(int j = 0; j < X; j++) { if(s.board[i][j] != s.board[0][0]) { ok = false; break; } } } if(ok) { res = s.turn; break; } for(char c = 0; c < 3; c++) { if(s.board[0][0] == c) continue; memcpy(S.board, s.board, sizeof(s.board)); queue<P> Q2; Q2.emplace(0, 0); char prev = S.board[0][0]; S.board[0][0] = c; while(!Q2.empty()) { P p = Q2.front(); Q2.pop(); for(int i = 0; i < 4; i++) { const int dy[4] = { 0, -1, 0, 1 }; const int dx[4] = { 1, 0, -1, 0 }; int ny = p.first + dy[i]; int nx = p.second + dx[i]; if(0 <= ny && ny < Y && 0 <= nx && nx < X && S.board[ny][nx] == prev) { S.board[ny][nx] = c; Q2.emplace(ny, nx); } } } S.turn = s.turn + 1; Q.push(S); } } cout << res << endl; } } ```
#include <iostream> #include <queue> #include <cstring> #include <climits> using namespace std; typedef pair<int,int> P; int w, h, ans; int t[12][12]; bool closed[12][12]; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; bool judge(int array[12][12]){ for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ if(array[i][j] != array[0][0]){ return false; } } } return true; } void show(int s[12][12]){ for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ cout<<s[i][j]<<" "; } cout<<endl; } cout<<"--\n"; } void nextState(int from[12][12], int to1[12][12], int to2[12][12]){ int color1, color2; if(from[0][0] == 0) { color1 = 1; color2 = 2; } else if(from[0][0] == 1) { color1 = 0; color2 = 2; } else { color1 = 0; color2 = 1; } for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ to1[i][j] = to2[i][j] = from[i][j]; } } queue<P> open; open.push(P(0, 0)); memset(closed, 0, sizeof(closed)); to1[0][0] = color1; to2[0][0] = color2; while(!open.empty()){ P p = open.front(); open.pop(); for(int i = 0; i < 4; i++){ int nx = p.first + dx[i]; int ny = p.second + dy[i]; if(0 <= nx && nx < w && 0 <= ny && ny < h && !closed[ny][nx] && from[ny][nx] == from[0][0]){ to1[ny][nx] = color1; to2[ny][nx] = color2; closed[ny][nx] = true; open.push(P(nx, ny)); } } } } void solve(int s[12][12], int cnt){ if(judge(s)){ ans = min(ans, cnt); return; } if(cnt >= ans){ return; } int next1[12][12]; int next2[12][12]; nextState(s, next1, next2); solve(next1, cnt + 1); solve(next2, cnt + 1); } int main(){ while(cin >> w >> h, w || h){ for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ char ch; cin >> ch; if(ch == 'R') t[i][j] = 0; else if(ch == 'G') t[i][j] = 1; else if(ch == 'B') t[i][j] = 2; } } ans = 18; solve(t, 0); cout << ans << endl; } }
### Prompt Develop a solution in CPP to the problem described below: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <iostream> #include <queue> #include <cstring> #include <climits> using namespace std; typedef pair<int,int> P; int w, h, ans; int t[12][12]; bool closed[12][12]; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; bool judge(int array[12][12]){ for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ if(array[i][j] != array[0][0]){ return false; } } } return true; } void show(int s[12][12]){ for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ cout<<s[i][j]<<" "; } cout<<endl; } cout<<"--\n"; } void nextState(int from[12][12], int to1[12][12], int to2[12][12]){ int color1, color2; if(from[0][0] == 0) { color1 = 1; color2 = 2; } else if(from[0][0] == 1) { color1 = 0; color2 = 2; } else { color1 = 0; color2 = 1; } for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ to1[i][j] = to2[i][j] = from[i][j]; } } queue<P> open; open.push(P(0, 0)); memset(closed, 0, sizeof(closed)); to1[0][0] = color1; to2[0][0] = color2; while(!open.empty()){ P p = open.front(); open.pop(); for(int i = 0; i < 4; i++){ int nx = p.first + dx[i]; int ny = p.second + dy[i]; if(0 <= nx && nx < w && 0 <= ny && ny < h && !closed[ny][nx] && from[ny][nx] == from[0][0]){ to1[ny][nx] = color1; to2[ny][nx] = color2; closed[ny][nx] = true; open.push(P(nx, ny)); } } } } void solve(int s[12][12], int cnt){ if(judge(s)){ ans = min(ans, cnt); return; } if(cnt >= ans){ return; } int next1[12][12]; int next2[12][12]; nextState(s, next1, next2); solve(next1, cnt + 1); solve(next2, cnt + 1); } int main(){ while(cin >> w >> h, w || h){ for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ char ch; cin >> ch; if(ch == 'R') t[i][j] = 0; else if(ch == 'G') t[i][j] = 1; else if(ch == 'B') t[i][j] = 2; } } ans = 18; solve(t, 0); cout << ans << endl; } } ```
#include<iostream> #include<sstream> #include<string> #include<queue> #include<utility> using namespace std; typedef pair<string, int> P; int W, H, N, dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}; string rgb = "RGB"; bool check(const string& s) { char c = s[0]; for(int i = 0; i < W * H; i++) if(s[i] != c) return false; return true; } string f(char c, string s) { char a = s[0]; N = 0; queue<int> q; q.push(0); q.push(0); while(!q.empty()) { int x = q.front(); q.pop(); int y = q.front(); q.pop(); if(s[y * W + x] == c) continue; s[y * W + x] = c; N++; for(int i = 0; i < 4; i++) { int tx = x + dx[i], ty = y + dy[i]; if(tx >= 0 && tx < W && ty >= 0 && ty < H && s[ty * W + tx] == a) { q.push(tx); q.push(ty); } } } return s; } int main() { loop: stringstream ss; string s; cin >> W >> H; if(!W) return 0; for(int y = 0; y < H; y++) { for(int x = 0; x < W; x++) { string tmp; cin >> tmp; ss << tmp; } } s = ss.str(); queue<P> q; q.push(make_pair(s, 0)); while(!q.empty()) { P p = q.front(); q.pop(); char c = p.first[0]; for(int i = 0; i < 3; i++) { if(c != rgb[i]) { q.push(make_pair(f(rgb[i], p.first), p.second + 1)); if(N == W * H) { cout << p.second << endl; goto loop; } } } } }
### Prompt Your task is to create a cpp solution to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include<iostream> #include<sstream> #include<string> #include<queue> #include<utility> using namespace std; typedef pair<string, int> P; int W, H, N, dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}; string rgb = "RGB"; bool check(const string& s) { char c = s[0]; for(int i = 0; i < W * H; i++) if(s[i] != c) return false; return true; } string f(char c, string s) { char a = s[0]; N = 0; queue<int> q; q.push(0); q.push(0); while(!q.empty()) { int x = q.front(); q.pop(); int y = q.front(); q.pop(); if(s[y * W + x] == c) continue; s[y * W + x] = c; N++; for(int i = 0; i < 4; i++) { int tx = x + dx[i], ty = y + dy[i]; if(tx >= 0 && tx < W && ty >= 0 && ty < H && s[ty * W + tx] == a) { q.push(tx); q.push(ty); } } } return s; } int main() { loop: stringstream ss; string s; cin >> W >> H; if(!W) return 0; for(int y = 0; y < H; y++) { for(int x = 0; x < W; x++) { string tmp; cin >> tmp; ss << tmp; } } s = ss.str(); queue<P> q; q.push(make_pair(s, 0)); while(!q.empty()) { P p = q.front(); q.pop(); char c = p.first[0]; for(int i = 0; i < 3; i++) { if(c != rgb[i]) { q.push(make_pair(f(rgb[i], p.first), p.second + 1)); if(N == W * H) { cout << p.second << endl; goto loop; } } } } } ```
#include<iostream> #include<cstring> using namespace std; #define INF (1<<29) #define rep(i,n) for(int i=0;i<(n);++i) int w,h; int ans; int dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; bool visit[10][10]; inline bool check(int y,int x){ return 0<=y&&y<h&&0<=x&&x<w; } int neighbor(int y,int x,char color,char panel[10][10]){ int res=0; if(!check(y,x) || visit[y][x])return 0; if(panel[y][x]!=color){ if(panel[y][x]=='R')return 1; if(panel[y][x]=='G')return 2; if(panel[y][x]=='B')return 4; } visit[y][x] = true; rep(i,4){ res |= neighbor(y+dy[i],x+dx[i],color,panel); } return res; } void change(int y,int x,char from,char to,char panel[10][10]){ if(!check(y,x) || from!=panel[y][x])return; panel[y][x] = to; rep(i,4){ change(y+dy[i],x+dx[i],from,to,panel); } } bool find; bool search(int depth,int limit,char panel[10][10]){ memset(visit,false,sizeof(visit)); int diff = neighbor(0,0,panel[0][0],panel); if(diff==0){ return true;; } if(limit <= depth)return false; char buf[10][10]; rep(i,3){ if((diff&1<<i)==0)continue; memcpy(buf,panel,sizeof(buf)); change(0,0,buf[0][0], (i==0?'R':(i==1?'G':'B')),buf); if(search(depth+1,limit,buf))return true; } return false; } int main(){ char panel[10][10]; while(cin>>w>>h,w|h){ rep(i,h)rep(j,w)cin>>panel[i][j]; int l=0,r=10000,m; while(l<r){ m=(l+r)/2; if(search(0,m,panel))r=m; else l=m+1; } cout<<r<<endl; } }
### Prompt In CPP, your task is to solve the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include<iostream> #include<cstring> using namespace std; #define INF (1<<29) #define rep(i,n) for(int i=0;i<(n);++i) int w,h; int ans; int dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; bool visit[10][10]; inline bool check(int y,int x){ return 0<=y&&y<h&&0<=x&&x<w; } int neighbor(int y,int x,char color,char panel[10][10]){ int res=0; if(!check(y,x) || visit[y][x])return 0; if(panel[y][x]!=color){ if(panel[y][x]=='R')return 1; if(panel[y][x]=='G')return 2; if(panel[y][x]=='B')return 4; } visit[y][x] = true; rep(i,4){ res |= neighbor(y+dy[i],x+dx[i],color,panel); } return res; } void change(int y,int x,char from,char to,char panel[10][10]){ if(!check(y,x) || from!=panel[y][x])return; panel[y][x] = to; rep(i,4){ change(y+dy[i],x+dx[i],from,to,panel); } } bool find; bool search(int depth,int limit,char panel[10][10]){ memset(visit,false,sizeof(visit)); int diff = neighbor(0,0,panel[0][0],panel); if(diff==0){ return true;; } if(limit <= depth)return false; char buf[10][10]; rep(i,3){ if((diff&1<<i)==0)continue; memcpy(buf,panel,sizeof(buf)); change(0,0,buf[0][0], (i==0?'R':(i==1?'G':'B')),buf); if(search(depth+1,limit,buf))return true; } return false; } int main(){ char panel[10][10]; while(cin>>w>>h,w|h){ rep(i,h)rep(j,w)cin>>panel[i][j]; int l=0,r=10000,m; while(l<r){ m=(l+r)/2; if(search(0,m,panel))r=m; else l=m+1; } cout<<r<<endl; } } ```
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <queue> #include <iterator> #include <map> #define REP(i,k,n) for(int i=k;i<n;i++) #define rep(i,n) for(int i=0;i<n;i++) using namespace std; int dx[4] = {1,0,-1,0}; int dy[4] = {0,1,0,-1}; struct Field { vector<vector<char> > f; int cnt; }; bool check(vector<vector<char> > v,int y,int x) { char ret = v[0][0]; rep(i,y) { rep(j,x) { if(ret != v[i][j]) return false; } } return true; } void change(vector<vector<char> > &v,int y,int x,char color) { bool used[10][10]; rep(i,10) rep(j,10) used[i][j] = false; queue<pair<pair<int,int>,char> > que; que.push(make_pair(make_pair(0,0),v[0][0])); used[0][0] = true; v[0][0] = color; while(que.size()) { int nowy = que.front().first.first; int nowx = que.front().first.second; char nowc = que.front().second; que.pop(); rep(i,4) { int ny = nowy + dy[i]; int nx = nowx + dx[i]; if(0 <= ny && ny < y && 0 <= nx && nx < x && !used[ny][nx]) { char nc = v[ny][nx]; if(nc == nowc) { used[ny][nx] = true; v[ny][nx] = color; que.push(make_pair(make_pair(ny,nx),nc)); } } } } } int main() { int x,y; while(cin >> x >> y) { if(x == 0 && y == 0) break; vector<vector<char> > f; f.resize(y); rep(i,y) { rep(j,x) { char s; cin >> s; f[i].push_back(s); } } Field field; field.f.resize(y); rep(i,y) copy(f[i].begin(),f[i].end(),back_inserter(field.f[i])); field.cnt = 0; queue<Field> que; que.push(field); int ans = 0; map<vector<vector<char> >,int> ma; while(que.size()) { Field field = que.front(); que.pop(); if(check(field.f,y,x)) { ans = field.cnt; break; } if(ma[field.f] == 0) { ma[field.f]++; Field next; next.f.resize(y); rep(i,y) copy(field.f[i].begin(),field.f[i].end(),back_inserter(next.f[i])); next.cnt = field.cnt + 1; change(next.f,y,x,'R'); que.push(next); rep(i,y) next.f[i].clear(); rep(i,y) copy(field.f[i].begin(),field.f[i].end(),back_inserter(next.f[i])); next.cnt = field.cnt + 1; change(next.f,y,x,'G'); que.push(next); rep(i,y) next.f[i].clear(); rep(i,y) copy(field.f[i].begin(),field.f[i].end(),back_inserter(next.f[i])); next.cnt = field.cnt + 1; change(next.f,y,x,'B'); que.push(next); } } cout << ans << endl; } }
### Prompt Develop a solution in CPP to the problem described below: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <iostream> #include <string> #include <vector> #include <algorithm> #include <queue> #include <iterator> #include <map> #define REP(i,k,n) for(int i=k;i<n;i++) #define rep(i,n) for(int i=0;i<n;i++) using namespace std; int dx[4] = {1,0,-1,0}; int dy[4] = {0,1,0,-1}; struct Field { vector<vector<char> > f; int cnt; }; bool check(vector<vector<char> > v,int y,int x) { char ret = v[0][0]; rep(i,y) { rep(j,x) { if(ret != v[i][j]) return false; } } return true; } void change(vector<vector<char> > &v,int y,int x,char color) { bool used[10][10]; rep(i,10) rep(j,10) used[i][j] = false; queue<pair<pair<int,int>,char> > que; que.push(make_pair(make_pair(0,0),v[0][0])); used[0][0] = true; v[0][0] = color; while(que.size()) { int nowy = que.front().first.first; int nowx = que.front().first.second; char nowc = que.front().second; que.pop(); rep(i,4) { int ny = nowy + dy[i]; int nx = nowx + dx[i]; if(0 <= ny && ny < y && 0 <= nx && nx < x && !used[ny][nx]) { char nc = v[ny][nx]; if(nc == nowc) { used[ny][nx] = true; v[ny][nx] = color; que.push(make_pair(make_pair(ny,nx),nc)); } } } } } int main() { int x,y; while(cin >> x >> y) { if(x == 0 && y == 0) break; vector<vector<char> > f; f.resize(y); rep(i,y) { rep(j,x) { char s; cin >> s; f[i].push_back(s); } } Field field; field.f.resize(y); rep(i,y) copy(f[i].begin(),f[i].end(),back_inserter(field.f[i])); field.cnt = 0; queue<Field> que; que.push(field); int ans = 0; map<vector<vector<char> >,int> ma; while(que.size()) { Field field = que.front(); que.pop(); if(check(field.f,y,x)) { ans = field.cnt; break; } if(ma[field.f] == 0) { ma[field.f]++; Field next; next.f.resize(y); rep(i,y) copy(field.f[i].begin(),field.f[i].end(),back_inserter(next.f[i])); next.cnt = field.cnt + 1; change(next.f,y,x,'R'); que.push(next); rep(i,y) next.f[i].clear(); rep(i,y) copy(field.f[i].begin(),field.f[i].end(),back_inserter(next.f[i])); next.cnt = field.cnt + 1; change(next.f,y,x,'G'); que.push(next); rep(i,y) next.f[i].clear(); rep(i,y) copy(field.f[i].begin(),field.f[i].end(),back_inserter(next.f[i])); next.cnt = field.cnt + 1; change(next.f,y,x,'B'); que.push(next); } } cout << ans << endl; } } ```
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<(int)(n);i++) const int dy[]={-1,0,1,0},dx[]={0,-1,0,1}; int w,h; char fld[20][20]; int lim; void paint(int y,int x,char c,vector<int>&V){ char tmp=fld[y][x]; fld[y][x]=c; V.push_back(y*w+x); rep(d,4){ int ny=y+dy[d],nx=x+dx[d]; if(ny<0||ny>=h||nx<0||nx>=w)continue; if(fld[ny][nx]!=tmp)continue; paint(ny,nx,c,V); } } bool dfs(int cnt){ char c=fld[0][0]; bool ok=1; rep(i,h)rep(j,w)if(fld[i][j]!=c)ok=0; if(ok)return 1; if(cnt>=lim)return 0; rep(i,3)if(c!="RGB"[i]){ vector<int>V; paint(0,0,"RGB"[i],V); if(dfs(cnt+1))return 1; rep(j,V.size())fld[V[j]/w][V[j]%w]=c; } return 0; } int main(){ while(cin>>w>>h,w||h){ rep(i,h)rep(j,w)cin>>fld[i][j]; for(lim=0;;lim++){ if(dfs(0))break; } cout<<lim<<endl; } return 0; }
### Prompt Please create a solution in CPP to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include<bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<(int)(n);i++) const int dy[]={-1,0,1,0},dx[]={0,-1,0,1}; int w,h; char fld[20][20]; int lim; void paint(int y,int x,char c,vector<int>&V){ char tmp=fld[y][x]; fld[y][x]=c; V.push_back(y*w+x); rep(d,4){ int ny=y+dy[d],nx=x+dx[d]; if(ny<0||ny>=h||nx<0||nx>=w)continue; if(fld[ny][nx]!=tmp)continue; paint(ny,nx,c,V); } } bool dfs(int cnt){ char c=fld[0][0]; bool ok=1; rep(i,h)rep(j,w)if(fld[i][j]!=c)ok=0; if(ok)return 1; if(cnt>=lim)return 0; rep(i,3)if(c!="RGB"[i]){ vector<int>V; paint(0,0,"RGB"[i],V); if(dfs(cnt+1))return 1; rep(j,V.size())fld[V[j]/w][V[j]%w]=c; } return 0; } int main(){ while(cin>>w>>h,w||h){ rep(i,h)rep(j,w)cin>>fld[i][j]; for(lim=0;;lim++){ if(dfs(0))break; } cout<<lim<<endl; } return 0; } ```
#include <cstdio> #include <cstdint> #include <vector> #include <algorithm> #include <string> #include <utility> #include <tuple> #include <queue> constexpr size_t m1 = -1; constexpr size_t di[] = {m1, 0, 1, 0}; constexpr size_t dj[] = {0, m1, 0, 1}; using ban = std::vector<std::string>; using zahyo = std::pair<size_t, size_t>; bool nuru(ban& t, char c) { char c0 = t[0][0]; size_t h = t.size(); size_t w = t[0].length(); std::queue<zahyo> q; q.emplace(0, 0); t[0][0] = c; while (!q.empty()) { size_t i, j; std::tie(i, j) = q.front(); q.pop(); for (int k = 0; k < 4; ++k) { size_t ni = i + di[k]; size_t nj = j + dj[k]; if (!(ni < h && nj < w)) continue; if (t[ni][nj] != c0) continue; t[ni][nj] = c; q.emplace(ni, nj); } } size_t res = 0; for (size_t i = 0; i < h; ++i) for (size_t j = 0; j < w; ++j) if (t[i][j] == c) ++res; return (res == h * w); } int testcase_ends() { size_t H, W; scanf("%zu %zu", &W, &H); if (H == 0 && W == 0) return 1; std::vector<std::string> s(H, std::string(W, '@')); for (size_t i = 0; i < H; ++i) for (size_t j = 0; j < W; ++j) scanf(" %c", &s[i][j]); { size_t count = 0; for (size_t i = 0; i < H; ++i) for (size_t j = 0; j < W; ++j) if (s[0][0] == s[i][j]) ++count; if (count == H * W) return puts("0"), 0; } std::string const color = "RGB"; std::queue<std::pair<ban, int>> q; q.emplace(s, 0); while (!q.empty()) { ban b; size_t res; std::tie(b, res) = q.front(); q.pop(); for (char c: color) { if (b[0][0] == c) continue; ban t = b; if (nuru(t, c)) return !printf("%zu\n", res+1); q.emplace(t, res+1); } } return 0; } int main() { while (!testcase_ends()) {} }
### Prompt Create a solution in CPP for the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <cstdio> #include <cstdint> #include <vector> #include <algorithm> #include <string> #include <utility> #include <tuple> #include <queue> constexpr size_t m1 = -1; constexpr size_t di[] = {m1, 0, 1, 0}; constexpr size_t dj[] = {0, m1, 0, 1}; using ban = std::vector<std::string>; using zahyo = std::pair<size_t, size_t>; bool nuru(ban& t, char c) { char c0 = t[0][0]; size_t h = t.size(); size_t w = t[0].length(); std::queue<zahyo> q; q.emplace(0, 0); t[0][0] = c; while (!q.empty()) { size_t i, j; std::tie(i, j) = q.front(); q.pop(); for (int k = 0; k < 4; ++k) { size_t ni = i + di[k]; size_t nj = j + dj[k]; if (!(ni < h && nj < w)) continue; if (t[ni][nj] != c0) continue; t[ni][nj] = c; q.emplace(ni, nj); } } size_t res = 0; for (size_t i = 0; i < h; ++i) for (size_t j = 0; j < w; ++j) if (t[i][j] == c) ++res; return (res == h * w); } int testcase_ends() { size_t H, W; scanf("%zu %zu", &W, &H); if (H == 0 && W == 0) return 1; std::vector<std::string> s(H, std::string(W, '@')); for (size_t i = 0; i < H; ++i) for (size_t j = 0; j < W; ++j) scanf(" %c", &s[i][j]); { size_t count = 0; for (size_t i = 0; i < H; ++i) for (size_t j = 0; j < W; ++j) if (s[0][0] == s[i][j]) ++count; if (count == H * W) return puts("0"), 0; } std::string const color = "RGB"; std::queue<std::pair<ban, int>> q; q.emplace(s, 0); while (!q.empty()) { ban b; size_t res; std::tie(b, res) = q.front(); q.pop(); for (char c: color) { if (b[0][0] == c) continue; ban t = b; if (nuru(t, c)) return !printf("%zu\n", res+1); q.emplace(t, res+1); } } return 0; } int main() { while (!testcase_ends()) {} } ```
#include<bits/stdc++.h> using namespace std; const int dx[]={-1,0,1,0}; const int dy[]={0,-1,0,1}; int H,W; int fld[10][10]; int lim; bool change(int y,int x,int c,int f){ fld[y][x]=c; int cnt=1; for(int d=0;d<4;d++){ int ny=y+dy[d],nx=x+dx[d]; if(ny<0||ny>=H||nx<0||nx>=W||fld[ny][nx]!=f)continue; cnt+=change(ny,nx,c,f); } return cnt; } bool dfs(int t=0,int sum=1){ if(lim==t){ bool ok=true; for(int i=0;i<H;i++)for(int j=0;j<W;j++){ if(fld[0][0]!=fld[i][j])ok=false; } return ok; } int _fld[10][10]; memcpy(_fld,fld,sizeof(_fld)); bool ret=false; for(int k=0;k<3;k++)if(k!=fld[0][0]){ int la=change(0,0,k,fld[0][0]); if(la>=sum)ret|=dfs(t+1,la); memcpy(fld,_fld,sizeof(fld)); } return ret; } int main(){ while(cin>>W>>H,W||H){ for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ char c; cin>>c; switch(c){ case 'R':fld[i][j]=0;break; case 'G':fld[i][j]=1;break; case 'B':fld[i][j]=2;break; } } } for(lim=0;;lim++)if(dfs())break; cout<<lim<<endl; } return 0; }
### Prompt Generate a CPP solution to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include<bits/stdc++.h> using namespace std; const int dx[]={-1,0,1,0}; const int dy[]={0,-1,0,1}; int H,W; int fld[10][10]; int lim; bool change(int y,int x,int c,int f){ fld[y][x]=c; int cnt=1; for(int d=0;d<4;d++){ int ny=y+dy[d],nx=x+dx[d]; if(ny<0||ny>=H||nx<0||nx>=W||fld[ny][nx]!=f)continue; cnt+=change(ny,nx,c,f); } return cnt; } bool dfs(int t=0,int sum=1){ if(lim==t){ bool ok=true; for(int i=0;i<H;i++)for(int j=0;j<W;j++){ if(fld[0][0]!=fld[i][j])ok=false; } return ok; } int _fld[10][10]; memcpy(_fld,fld,sizeof(_fld)); bool ret=false; for(int k=0;k<3;k++)if(k!=fld[0][0]){ int la=change(0,0,k,fld[0][0]); if(la>=sum)ret|=dfs(t+1,la); memcpy(fld,_fld,sizeof(fld)); } return ret; } int main(){ while(cin>>W>>H,W||H){ for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ char c; cin>>c; switch(c){ case 'R':fld[i][j]=0;break; case 'G':fld[i][j]=1;break; case 'B':fld[i][j]=2;break; } } } for(lim=0;;lim++)if(dfs())break; cout<<lim<<endl; } return 0; } ```
#include<bits/stdc++.h> using namespace std; int X, Y, Ans; int Dx[] = { 1,0,-1,0 }; int Dy[] = { 0,1,0,-1 }; bool Used[10][10]; void show(int s[10][10]) { char ch; for (int i = 0; i < Y; i++) { for (int j = 0; j < X; j++) { if (s[i][j] == 0) { ch = 'R'; } else if (s[i][j] == 1) { ch = 'G'; } else { ch = 'B'; } cout << ch << " "; } cout << endl; } cout << "--\n"; } bool judge(int(*p)[10]) { for (int i = 0; i < Y; ++i) { for (int j = 0; j < X; ++j) { if (p[i][j] != p[0][0]) { return false; } } } return true; } void nextState(int (*pSource)[10], int (*pTo1)[10], int (*pTo2)[10]) { int color1, color2, nowX, nowY; queue<pair<int, int> > convert; if (pSource[0][0] == 0) { color1 = 1; color2 = 2; } else if (pSource[0][0] == 1) { color1 = 0; color2 = 2; } else { color1 = 0; color2 = 1; } for (int i = 0; i < Y; ++i) { for (int j = 0; j < X; ++j) { pTo1[i][j] = pTo2[i][j] = pSource[i][j]; } } memset(Used, false, sizeof(Used)); convert.push(make_pair(0, 0)); Used[0][0] = true; pTo1[0][0] = color1; pTo2[0][0] = color2; while (!convert.empty()) { pair<int, int> temp; temp = convert.front(); convert.pop(); for (int i = 0; i < 4; ++i) { nowX = temp.first + Dx[i]; nowY = temp.second + Dy[i]; if (nowX<0 || nowX>=X || nowY<0 || nowY>=Y) { continue; } if (Used[nowY][nowX]) { continue; } if (pSource[nowY][nowX] != pSource[0][0]) { continue; } Used[nowY][nowX] = true; pTo1[nowY][nowX] = color1; pTo2[nowY][nowX] = color2; convert.push(make_pair(nowX, nowY)); } } } void solve(int (*p)[10], int cnt) { if (judge(p)) { Ans = min(Ans, cnt); return; } if (cnt >= Ans) { return; } int Tmp1[10][10], Tmp2[10][10]; nextState(p, Tmp1, Tmp2); solve(Tmp1, cnt + 1); solve(Tmp2, cnt + 1); } int main() { char c; int mapData[10][10]; while (cin >> X >> Y, X) { for (int i = 0; i < Y; ++i) { for (int j = 0; j < X; ++j) { cin >> c; if (c == 'R') { mapData[i][j] = 0; } else if (c == 'G') { mapData[i][j] = 1; } else if (c == 'B') { mapData[i][j] = 2; } } } //show(mapData); Ans = 20; solve(mapData, 0); cout << Ans << endl; } return 0; }
### Prompt Generate a cpp solution to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int X, Y, Ans; int Dx[] = { 1,0,-1,0 }; int Dy[] = { 0,1,0,-1 }; bool Used[10][10]; void show(int s[10][10]) { char ch; for (int i = 0; i < Y; i++) { for (int j = 0; j < X; j++) { if (s[i][j] == 0) { ch = 'R'; } else if (s[i][j] == 1) { ch = 'G'; } else { ch = 'B'; } cout << ch << " "; } cout << endl; } cout << "--\n"; } bool judge(int(*p)[10]) { for (int i = 0; i < Y; ++i) { for (int j = 0; j < X; ++j) { if (p[i][j] != p[0][0]) { return false; } } } return true; } void nextState(int (*pSource)[10], int (*pTo1)[10], int (*pTo2)[10]) { int color1, color2, nowX, nowY; queue<pair<int, int> > convert; if (pSource[0][0] == 0) { color1 = 1; color2 = 2; } else if (pSource[0][0] == 1) { color1 = 0; color2 = 2; } else { color1 = 0; color2 = 1; } for (int i = 0; i < Y; ++i) { for (int j = 0; j < X; ++j) { pTo1[i][j] = pTo2[i][j] = pSource[i][j]; } } memset(Used, false, sizeof(Used)); convert.push(make_pair(0, 0)); Used[0][0] = true; pTo1[0][0] = color1; pTo2[0][0] = color2; while (!convert.empty()) { pair<int, int> temp; temp = convert.front(); convert.pop(); for (int i = 0; i < 4; ++i) { nowX = temp.first + Dx[i]; nowY = temp.second + Dy[i]; if (nowX<0 || nowX>=X || nowY<0 || nowY>=Y) { continue; } if (Used[nowY][nowX]) { continue; } if (pSource[nowY][nowX] != pSource[0][0]) { continue; } Used[nowY][nowX] = true; pTo1[nowY][nowX] = color1; pTo2[nowY][nowX] = color2; convert.push(make_pair(nowX, nowY)); } } } void solve(int (*p)[10], int cnt) { if (judge(p)) { Ans = min(Ans, cnt); return; } if (cnt >= Ans) { return; } int Tmp1[10][10], Tmp2[10][10]; nextState(p, Tmp1, Tmp2); solve(Tmp1, cnt + 1); solve(Tmp2, cnt + 1); } int main() { char c; int mapData[10][10]; while (cin >> X >> Y, X) { for (int i = 0; i < Y; ++i) { for (int j = 0; j < X; ++j) { cin >> c; if (c == 'R') { mapData[i][j] = 0; } else if (c == 'G') { mapData[i][j] = 1; } else if (c == 'B') { mapData[i][j] = 2; } } } //show(mapData); Ans = 20; solve(mapData, 0); cout << Ans << endl; } return 0; } ```
#include<bits/stdc++.h> using namespace std; typedef struct{ int maps[11][11]; int c; }DAT; typedef pair<int,int>P; DAT tmp; int x,y; int dy[4] = {1,0,-1,0}; int dx[4] = {0,1,0,-1}; string str; int solve(DAT); int change_color(DAT,int); int main(){ char moji; while(cin >> x >> y,x|y){ DAT a; for(int i = 0; i < y; i++){ for(int j = 0; j < x; j++){ cin >> moji; if(moji == 'R'){ a.maps[i][j] = 1; }else if(moji == 'G'){ a.maps[i][j] = 2; }else{ a.maps[i][j] = 3; } } } a.c = 0; int cnt = 0; int f = 0; for(int k = 1; k <= 3; k++){ for(int i = 0 ; i < y; i++){ for(int j = 0; j < x; j++){ if(a.maps[i][j] == k)cnt++; } } if(cnt == y*x){ cout << 0 << endl; f = 1; } cnt = 0; } if(f == 0){ cout << solve(DAT(a)) << endl; } } } int solve(DAT data){ queue<DAT> que; map<string,int> check; que.push(data); while(!que.empty()){ DAT d = que.front();que.pop(); for(int i = 1; i <= 3; i++){ if(i != d.maps[0][0]){ if(change_color(d,i)){ return d.c + 1; } if(check.count(str) == 0){ check[str] = 1; que.push(tmp); } } } } return -1; } int change_color(DAT data,int color){ //隣接する場所の色を変える(tmpを操作) for(int i = 0; i < y; i++){ for(int j = 0; j < x; j++){ tmp.maps[i][j] = data.maps[i][j]; } } //cnt++ tmp.c = data.c + 1; int same = tmp.maps[0][0]; queue<P> que; que.push(P(0,0)); while(!que.empty()){ P p = que.front();que.pop(); tmp.maps[p.first][p.second] = color; for(int i = 0; i < 4; i++){ int ny = p.first + dy[i]; int nx = p.second + dx[i]; if(tmp.maps[ny][nx] == same && ny >= 0 && ny < y && nx >= 0 && nx < x ){ que.push(P(ny,nx)); } } } //stringにする str = ""; for(int i = 0; i < y; i++){ for(int j = 0; j < x; j++){ str += tmp.maps[i][j] + '0'; } } int cnt = 0; for(int i = 0 ; i < y; i++){ for(int j = 0; j < x; j++){ if(tmp.maps[i][j] == color)cnt++; } } //マップをみてそろってたら1を返す if(cnt == y * x)return 1; else return 0; }
### Prompt Generate a Cpp solution to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include<bits/stdc++.h> using namespace std; typedef struct{ int maps[11][11]; int c; }DAT; typedef pair<int,int>P; DAT tmp; int x,y; int dy[4] = {1,0,-1,0}; int dx[4] = {0,1,0,-1}; string str; int solve(DAT); int change_color(DAT,int); int main(){ char moji; while(cin >> x >> y,x|y){ DAT a; for(int i = 0; i < y; i++){ for(int j = 0; j < x; j++){ cin >> moji; if(moji == 'R'){ a.maps[i][j] = 1; }else if(moji == 'G'){ a.maps[i][j] = 2; }else{ a.maps[i][j] = 3; } } } a.c = 0; int cnt = 0; int f = 0; for(int k = 1; k <= 3; k++){ for(int i = 0 ; i < y; i++){ for(int j = 0; j < x; j++){ if(a.maps[i][j] == k)cnt++; } } if(cnt == y*x){ cout << 0 << endl; f = 1; } cnt = 0; } if(f == 0){ cout << solve(DAT(a)) << endl; } } } int solve(DAT data){ queue<DAT> que; map<string,int> check; que.push(data); while(!que.empty()){ DAT d = que.front();que.pop(); for(int i = 1; i <= 3; i++){ if(i != d.maps[0][0]){ if(change_color(d,i)){ return d.c + 1; } if(check.count(str) == 0){ check[str] = 1; que.push(tmp); } } } } return -1; } int change_color(DAT data,int color){ //隣接する場所の色を変える(tmpを操作) for(int i = 0; i < y; i++){ for(int j = 0; j < x; j++){ tmp.maps[i][j] = data.maps[i][j]; } } //cnt++ tmp.c = data.c + 1; int same = tmp.maps[0][0]; queue<P> que; que.push(P(0,0)); while(!que.empty()){ P p = que.front();que.pop(); tmp.maps[p.first][p.second] = color; for(int i = 0; i < 4; i++){ int ny = p.first + dy[i]; int nx = p.second + dx[i]; if(tmp.maps[ny][nx] == same && ny >= 0 && ny < y && nx >= 0 && nx < x ){ que.push(P(ny,nx)); } } } //stringにする str = ""; for(int i = 0; i < y; i++){ for(int j = 0; j < x; j++){ str += tmp.maps[i][j] + '0'; } } int cnt = 0; for(int i = 0 ; i < y; i++){ for(int j = 0; j < x; j++){ if(tmp.maps[i][j] == color)cnt++; } } //マップをみてそろってたら1を返す if(cnt == y * x)return 1; else return 0; } ```
//15 #include<iostream> #include<algorithm> #include<queue> #include<vector> using namespace std; int x,y; struct S{ int t; vector<char> g; }; void ff(vector<char> &g,int xx,int yy,char c,char sc){ if(xx<0||x<=xx||yy<0||y<=yy||sc!=g[yy*x+xx])return; g[yy*x+xx]=c; for(int i=0;i<4;i++){ int d[]={0,1,0,-1,0}; ff(g,xx+d[i],yy+d[i+1],c,sc); } } int main(){ while(cin>>x>>y,x|y){ vector<char> g(y*x); for(int i=0;i<y*x;i++){ cin>>g[i]; } queue<S> que; S is={0,g}; que.push(is); for(;;){ S cs=que.front(); char l='Z',h='A'; for(int i=0;i<y*x;i++){ l=min(l,cs.g[i]); h=max(h,cs.g[i]); } if(l==h)break; que.pop(); for(int i=0;i<3;i++){ const char *c="RGB"; if(c[i]==cs.g[0])continue; S ns={cs.t+1,cs.g}; ff(ns.g,0,0,c[i],ns.g[0]); que.push(ns); } } cout<<que.front().t<<endl; } return 0; }
### Prompt Create a solution in CPP for the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp //15 #include<iostream> #include<algorithm> #include<queue> #include<vector> using namespace std; int x,y; struct S{ int t; vector<char> g; }; void ff(vector<char> &g,int xx,int yy,char c,char sc){ if(xx<0||x<=xx||yy<0||y<=yy||sc!=g[yy*x+xx])return; g[yy*x+xx]=c; for(int i=0;i<4;i++){ int d[]={0,1,0,-1,0}; ff(g,xx+d[i],yy+d[i+1],c,sc); } } int main(){ while(cin>>x>>y,x|y){ vector<char> g(y*x); for(int i=0;i<y*x;i++){ cin>>g[i]; } queue<S> que; S is={0,g}; que.push(is); for(;;){ S cs=que.front(); char l='Z',h='A'; for(int i=0;i<y*x;i++){ l=min(l,cs.g[i]); h=max(h,cs.g[i]); } if(l==h)break; que.pop(); for(int i=0;i<3;i++){ const char *c="RGB"; if(c[i]==cs.g[0])continue; S ns={cs.t+1,cs.g}; ff(ns.g,0,0,c[i],ns.g[0]); que.push(ns); } } cout<<que.front().t<<endl; } return 0; } ```
#include <stdio.h> #include <assert.h> #include <vector> #include <queue> #include <utility> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define mp make_pair int n, m; char f[16][16]; int g[32][16][16]; bool completed(int (*g)[16]) { rep (i, n) rep (j, m) if (g[i][j] != g[0][0]) return false; return true; } void rec(int (*g)[16], int x, int y, int from, int to) { if (g[x][y] != from) return ; g[x][y] = to; if (x > 0) rec(g, x-1, y, from, to); if (x < n-1) rec(g, x+1, y, from, to); if (y > 0) rec(g, x, y-1, from, to); if (y < m-1) rec(g, x, y+1, from, to); } bool iddfs(int dep, int cur) { if (dep == cur) return completed(g[cur]); rep (k, 3) if (k != g[cur][0][0]) { rep (i, n) rep (j, m) g[cur+1][i][j] = g[cur][i][j]; rec(g[cur+1], 0, 0, g[cur][0][0], k); if (iddfs(dep, cur+1)) return true; } return false; } int solve() { rep (i, n) rep(j, m) { if (f[i][j] == 'R') g[0][i][j] = 0; if (f[i][j] == 'G') g[0][i][j] = 1; if (f[i][j] == 'B') g[0][i][j] = 2; } rep (i, 32) if (iddfs(i, 0)) return i; assert(false); return -1; } int main() { for (;;) { scanf("%d%d", &m, &n); if (n == 0 && m == 0) return 0; rep (i, n) rep (j, m) scanf(" %c", f[i]+j); printf("%d\n", solve()); } }
### Prompt Develop a solution in cpp to the problem described below: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <stdio.h> #include <assert.h> #include <vector> #include <queue> #include <utility> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define mp make_pair int n, m; char f[16][16]; int g[32][16][16]; bool completed(int (*g)[16]) { rep (i, n) rep (j, m) if (g[i][j] != g[0][0]) return false; return true; } void rec(int (*g)[16], int x, int y, int from, int to) { if (g[x][y] != from) return ; g[x][y] = to; if (x > 0) rec(g, x-1, y, from, to); if (x < n-1) rec(g, x+1, y, from, to); if (y > 0) rec(g, x, y-1, from, to); if (y < m-1) rec(g, x, y+1, from, to); } bool iddfs(int dep, int cur) { if (dep == cur) return completed(g[cur]); rep (k, 3) if (k != g[cur][0][0]) { rep (i, n) rep (j, m) g[cur+1][i][j] = g[cur][i][j]; rec(g[cur+1], 0, 0, g[cur][0][0], k); if (iddfs(dep, cur+1)) return true; } return false; } int solve() { rep (i, n) rep(j, m) { if (f[i][j] == 'R') g[0][i][j] = 0; if (f[i][j] == 'G') g[0][i][j] = 1; if (f[i][j] == 'B') g[0][i][j] = 2; } rep (i, 32) if (iddfs(i, 0)) return i; assert(false); return -1; } int main() { for (;;) { scanf("%d%d", &m, &n); if (n == 0 && m == 0) return 0; rep (i, n) rep (j, m) scanf(" %c", f[i]+j); printf("%d\n", solve()); } } ```
#include <queue> #include <string> #include <vector> #include <iostream> #include <cstring> #include <map> #define REP(i,a,b) for(int i=(a);i<(b);++i) #define rep(i,n) REP(i,0,n) using namespace std; int n, m; int dr[] = { -1, 0, 1, 0 }, dc[] = { 0, 1, 0, -1 }; int used[12][12]; bool done(const vector<string>& v) { rep(i, n) rep(j, m) if (v[i][j] != v[0][0]) return false; return true; } void dfs(vector<string>& v, int r, int c, int x) { int t = v[r][c]; v[r][c] = x; used[r][c] = 1; rep(i, 4) { int nr = r + dr[i]; int nc = c + dc[i]; if (nr < 0 || nr >= n || nc < 0 || nc >= m) continue; if (used[nr][nc] || v[nr][nc] != t) continue; dfs(v, nr, nc, x); } } int main() { ios_base::sync_with_stdio(0); while (cin >> m >> n) { if (!(n | m)) break; vector<string> v(n, string(m, ' ')); rep(i, n) rep(j, m) cin >> v[i][j]; queue<vector<string> > q; map<vector<string>, int> num; q.push(v); num[v] = 0; const string rgb = "RGB"; int res = -1; while (!q.empty()) { vector<string> p = q.front(); q.pop(); if (done(p)) { res = num[p]; break; } rep(i, rgb.size()) { vector<string> next = p; memset(used, 0, sizeof(used)); dfs(next, 0, 0, rgb[i]); if (num.find(next) != num.end()) continue; q.push(next); num[next] = num[p] + 1; } } cout << res << endl; } return 0; }
### Prompt Please formulate a CPP solution to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <queue> #include <string> #include <vector> #include <iostream> #include <cstring> #include <map> #define REP(i,a,b) for(int i=(a);i<(b);++i) #define rep(i,n) REP(i,0,n) using namespace std; int n, m; int dr[] = { -1, 0, 1, 0 }, dc[] = { 0, 1, 0, -1 }; int used[12][12]; bool done(const vector<string>& v) { rep(i, n) rep(j, m) if (v[i][j] != v[0][0]) return false; return true; } void dfs(vector<string>& v, int r, int c, int x) { int t = v[r][c]; v[r][c] = x; used[r][c] = 1; rep(i, 4) { int nr = r + dr[i]; int nc = c + dc[i]; if (nr < 0 || nr >= n || nc < 0 || nc >= m) continue; if (used[nr][nc] || v[nr][nc] != t) continue; dfs(v, nr, nc, x); } } int main() { ios_base::sync_with_stdio(0); while (cin >> m >> n) { if (!(n | m)) break; vector<string> v(n, string(m, ' ')); rep(i, n) rep(j, m) cin >> v[i][j]; queue<vector<string> > q; map<vector<string>, int> num; q.push(v); num[v] = 0; const string rgb = "RGB"; int res = -1; while (!q.empty()) { vector<string> p = q.front(); q.pop(); if (done(p)) { res = num[p]; break; } rep(i, rgb.size()) { vector<string> next = p; memset(used, 0, sizeof(used)); dfs(next, 0, 0, rgb[i]); if (num.find(next) != num.end()) continue; q.push(next); num[next] = num[p] + 1; } } cout << res << endl; } return 0; } ```
#include<iostream> #include<queue> using namespace std; class state { public: int c[12][12]; int time; }; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; int X, Y; void DFS(state& copy,int x,int y,int oldcolor,int newcolor) { if (copy.c[x][y] != oldcolor) return; else{ copy.c[x][y] = newcolor; } for (int i = 0; i < 4; i++) { DFS(copy, x + dx[i], y + dy[i], oldcolor,newcolor); } return; } int main() { while (true) { bool breakflg = false; state num; cin >> X >> Y; if (X == 0 && Y == 0) break; for (int x = 0; x < 12; x++) { for (int y = 0; y < 12; y++) { num.c[x][y] == -1; } } for (int y = 1; y <= Y; y++) { for (int x = 1; x <= X; x++) { char data; cin >> data; if (data == 'R') num.c[x][y] = 1; else if (data == 'G') num.c[x][y] = 2; else if (data == 'B') num.c[x][y] = 3; } } num.time = 0; queue<state> q; q.push(num); bool flg1 = true; for (int y = 1; y <= Y; y++) { for (int x = 1; x <= X; x++) { if (num.c[1][1] != num.c[x][y]) { flg1 = false; break; } } if (!flg1)break; } if (flg1) { cout << num.time << endl; continue; } while (true) { state tmp; tmp = q.front(); q.pop(); for (int i = 1; i <= 3; i++) { bool flg = true; state copy = tmp; if (i == copy.c[1][1])continue; else { DFS(copy, 1, 1, copy.c[1][1], i); copy.time++; for (int y = 1; y <= Y; y++) { for (int x = 1; x <= X; x++) { if (copy.c[1][1] != copy.c[x][y]) { flg = false; break; } } if (!flg)break; } if (flg) { cout << copy.time << endl; breakflg = true; break; } q.push(copy); } } if (breakflg) break; } } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include<iostream> #include<queue> using namespace std; class state { public: int c[12][12]; int time; }; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; int X, Y; void DFS(state& copy,int x,int y,int oldcolor,int newcolor) { if (copy.c[x][y] != oldcolor) return; else{ copy.c[x][y] = newcolor; } for (int i = 0; i < 4; i++) { DFS(copy, x + dx[i], y + dy[i], oldcolor,newcolor); } return; } int main() { while (true) { bool breakflg = false; state num; cin >> X >> Y; if (X == 0 && Y == 0) break; for (int x = 0; x < 12; x++) { for (int y = 0; y < 12; y++) { num.c[x][y] == -1; } } for (int y = 1; y <= Y; y++) { for (int x = 1; x <= X; x++) { char data; cin >> data; if (data == 'R') num.c[x][y] = 1; else if (data == 'G') num.c[x][y] = 2; else if (data == 'B') num.c[x][y] = 3; } } num.time = 0; queue<state> q; q.push(num); bool flg1 = true; for (int y = 1; y <= Y; y++) { for (int x = 1; x <= X; x++) { if (num.c[1][1] != num.c[x][y]) { flg1 = false; break; } } if (!flg1)break; } if (flg1) { cout << num.time << endl; continue; } while (true) { state tmp; tmp = q.front(); q.pop(); for (int i = 1; i <= 3; i++) { bool flg = true; state copy = tmp; if (i == copy.c[1][1])continue; else { DFS(copy, 1, 1, copy.c[1][1], i); copy.time++; for (int y = 1; y <= Y; y++) { for (int x = 1; x <= X; x++) { if (copy.c[1][1] != copy.c[x][y]) { flg = false; break; } } if (!flg)break; } if (flg) { cout << copy.time << endl; breakflg = true; break; } q.push(copy); } } if (breakflg) break; } } return 0; } ```
#include<iostream> #include<queue> #include<vector> using namespace std; #define rep(i,n) for(int i=0;i<n;i++) int w, h; void fill( vector<char> &f, char c, char ch, int x, int y ){ f[y*w+x] = ch; if( x>0 && f[y*w+(x-1)]==c ) fill( f, c, ch, x-1, y ); if( x+1<w && f[y*w+(x+1)]==c ) fill( f, c, ch, x+1, y ); if( y>0 && f[(y-1)*w+x]==c ) fill( f, c, ch, x, y-1 ); if( y+1<h && f[(y+1)*w+x]==c ) fill( f, c, ch, x, y+1 ); } int main(){ vector<char> field; queue< vector<char> > que; while( cin >> w >> h, w||h ){ while(!que.empty()) que.pop(); field.resize(w*h+1); rep(y,h) rep(x,w) cin >> field[y*w+x]; field[w*h] = 0; que.push(field); while(!que.empty()){ field = que.front(); que.pop(); int i=1; for(; i<w*h; i++) if(field[i]!=field[0]) break; if(i==w*h){ cout << (int)field[w*h] << endl; break; } if( field[0] != 'R' ){ vector<char> f = field; fill( f, field[0], 'R', 0, 0 ); f[w*h]++; que.push(f); } if( field[0] != 'G' ){ vector<char> f = field; fill( f, field[0], 'G', 0, 0 ); f[w*h]++; que.push(f); } if( field[0] != 'B' ){ vector<char> f = field; fill( f, field[0], 'B', 0, 0 ); f[w*h]++; que.push(f); } } } return 0; }
### Prompt Your task is to create a CPP solution to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include<iostream> #include<queue> #include<vector> using namespace std; #define rep(i,n) for(int i=0;i<n;i++) int w, h; void fill( vector<char> &f, char c, char ch, int x, int y ){ f[y*w+x] = ch; if( x>0 && f[y*w+(x-1)]==c ) fill( f, c, ch, x-1, y ); if( x+1<w && f[y*w+(x+1)]==c ) fill( f, c, ch, x+1, y ); if( y>0 && f[(y-1)*w+x]==c ) fill( f, c, ch, x, y-1 ); if( y+1<h && f[(y+1)*w+x]==c ) fill( f, c, ch, x, y+1 ); } int main(){ vector<char> field; queue< vector<char> > que; while( cin >> w >> h, w||h ){ while(!que.empty()) que.pop(); field.resize(w*h+1); rep(y,h) rep(x,w) cin >> field[y*w+x]; field[w*h] = 0; que.push(field); while(!que.empty()){ field = que.front(); que.pop(); int i=1; for(; i<w*h; i++) if(field[i]!=field[0]) break; if(i==w*h){ cout << (int)field[w*h] << endl; break; } if( field[0] != 'R' ){ vector<char> f = field; fill( f, field[0], 'R', 0, 0 ); f[w*h]++; que.push(f); } if( field[0] != 'G' ){ vector<char> f = field; fill( f, field[0], 'G', 0, 0 ); f[w*h]++; que.push(f); } if( field[0] != 'B' ){ vector<char> f = field; fill( f, field[0], 'B', 0, 0 ); f[w*h]++; que.push(f); } } } return 0; } ```
#include <iostream> #include <stdio.h> #include <sstream> #include <string> #include <vector> #include <map> #include <queue> #include <algorithm> #include <set> #include <math.h> #include <utility> #include <stack> #include <string.h> #include <complex> using namespace std; const int INF = 1<<29; const double EPS = 1e-8; //typedef vector<int> vec; typedef pair<int,int> P; struct edge{int to,cost;}; typedef vector<char> vec; typedef vector<vec> mat; const int dx[] = {0,0,1,-1}; const int dy[] = {1,-1,0,0}; int X, Y; struct State{ int d,pn; char c; mat f; bool operator<(const State& right) const{ return d == right.d ? pn < right.pn : d > right.d; } }; int paint(int y, int x, char c, char nc, mat& f){ f[y][x] = nc; int ret = 1; for(int i=0;i<4;i++){ int nx = x+dx[i], ny = y+dy[i]; if(nx<0||X<=nx||ny<0||Y<=ny||f[ny][nx]!=c) continue; ret += paint(ny, nx, c, nc, f); } return ret; } const char color[] = {'R','G','B'}; int main(){ while(cin >> X >> Y, X){ mat field(Y, vec(X)); for(int i=0;i<Y;i++){ for(int j=0;j<X;j++){ cin >> field[i][j]; } } priority_queue<State> que; int d[101];fill(d,d+101,INF); d[0] = 0; que.push((State){0,0,field[0][0],field}); bool ans_found = false; while(que.size()){ State p = que.top(); que.pop(); if(d[p.pn]<p.d) continue; for(int i=0;i<3;i++){ if(p.c==color[i])continue; mat nf = p.f; int npn = paint(0,0,p.f[0][0],color[i],nf); if(npn == X*Y){ ans_found = true; break; } if(npn <= p.pn) continue; if(d[npn]<p.d+1) continue; d[npn] = p.d+1; que.push((State){p.d+1,npn,color[i],nf}); } if(ans_found){ cout << p.d << endl; break; } } } return 0; }
### Prompt Please formulate a cpp solution to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <iostream> #include <stdio.h> #include <sstream> #include <string> #include <vector> #include <map> #include <queue> #include <algorithm> #include <set> #include <math.h> #include <utility> #include <stack> #include <string.h> #include <complex> using namespace std; const int INF = 1<<29; const double EPS = 1e-8; //typedef vector<int> vec; typedef pair<int,int> P; struct edge{int to,cost;}; typedef vector<char> vec; typedef vector<vec> mat; const int dx[] = {0,0,1,-1}; const int dy[] = {1,-1,0,0}; int X, Y; struct State{ int d,pn; char c; mat f; bool operator<(const State& right) const{ return d == right.d ? pn < right.pn : d > right.d; } }; int paint(int y, int x, char c, char nc, mat& f){ f[y][x] = nc; int ret = 1; for(int i=0;i<4;i++){ int nx = x+dx[i], ny = y+dy[i]; if(nx<0||X<=nx||ny<0||Y<=ny||f[ny][nx]!=c) continue; ret += paint(ny, nx, c, nc, f); } return ret; } const char color[] = {'R','G','B'}; int main(){ while(cin >> X >> Y, X){ mat field(Y, vec(X)); for(int i=0;i<Y;i++){ for(int j=0;j<X;j++){ cin >> field[i][j]; } } priority_queue<State> que; int d[101];fill(d,d+101,INF); d[0] = 0; que.push((State){0,0,field[0][0],field}); bool ans_found = false; while(que.size()){ State p = que.top(); que.pop(); if(d[p.pn]<p.d) continue; for(int i=0;i<3;i++){ if(p.c==color[i])continue; mat nf = p.f; int npn = paint(0,0,p.f[0][0],color[i],nf); if(npn == X*Y){ ans_found = true; break; } if(npn <= p.pn) continue; if(d[npn]<p.d+1) continue; d[npn] = p.d+1; que.push((State){p.d+1,npn,color[i],nf}); } if(ans_found){ cout << p.d << endl; break; } } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int x,y; void DFS(array<array<char,11>,11>& v,char base,char c,int xp,int yp){ if(xp < 0 || yp < 0 || xp >= x || yp >= y || v[xp][yp]!=base){ return; } v[xp][yp] = c; DFS(v,base,c,xp+1,yp); DFS(v,base,c,xp,yp+1); DFS(v,base,c,xp-1,yp); DFS(v,base,c,xp,yp-1); } int main() { while(1){ //scanf(" %d %d",&x,&y); cin >> x >> y; if(x==0 && y==0){ break; } array<array<char,11>,11> v; //v.cs.resize(static_cast<unsigned>(x)); for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ //char c; /*scanf(" %c",&c); v.cs[j][i] = c;*/ cin >> v[j][i]; //v.cs[j][i] = c; } } bool flag = true; for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ if(v[0][0] != v[j][i]){ flag = false; break; } } } if(flag){ cout << 0 << endl; continue; } queue<pair<array<array<char,11>,11>,int > > q; q.push(make_pair(v,0)); while(!q.empty()){ int count = q.front().second; v = q.front().first; /*for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ cout << v.cs[i][j] << ' '; }cout << endl; }cout << endl;*/ q.pop(); count++; array<array<char,11>,11> tmp; //tmp.resize(static_cast<unsigned>(x)); /*for(int i=0;i<y;i++){ tmp[i] += v.cs[i]; }*/ tmp = v; if(v[0][0] != 'R'){ DFS(v,v[0][0],'R',0,0); flag = true; for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ if('R' != v[j][i]){ flag = false; break; } } } if(flag){ cout << count << endl; break; } q.push(make_pair(v,count)); /*for(int i=0;i<y;i++){ v[i] = tmp[i]; }*/ v = tmp; } if(v[0][0] != 'G'){ DFS(v,v[0][0],'G',0,0); flag = true; for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ if('G' != v[j][i]){ flag = false; break; } } } if(flag){ cout << count << endl; break; } q.push(make_pair(v,count)); /*for(int i=0;i<y;i++){ v.cs[i] = tmp[i]; }*/ v = tmp; } if(v[0][0] != 'B'){ //char base = v.cs[0][0]; DFS(v,v[0][0],'B',0,0); flag = true; for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ if('B' != v[j][i]){ flag = false; break; } } } if(flag){ cout << count << endl; break; } q.push(make_pair(v,count)); } } } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int x,y; void DFS(array<array<char,11>,11>& v,char base,char c,int xp,int yp){ if(xp < 0 || yp < 0 || xp >= x || yp >= y || v[xp][yp]!=base){ return; } v[xp][yp] = c; DFS(v,base,c,xp+1,yp); DFS(v,base,c,xp,yp+1); DFS(v,base,c,xp-1,yp); DFS(v,base,c,xp,yp-1); } int main() { while(1){ //scanf(" %d %d",&x,&y); cin >> x >> y; if(x==0 && y==0){ break; } array<array<char,11>,11> v; //v.cs.resize(static_cast<unsigned>(x)); for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ //char c; /*scanf(" %c",&c); v.cs[j][i] = c;*/ cin >> v[j][i]; //v.cs[j][i] = c; } } bool flag = true; for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ if(v[0][0] != v[j][i]){ flag = false; break; } } } if(flag){ cout << 0 << endl; continue; } queue<pair<array<array<char,11>,11>,int > > q; q.push(make_pair(v,0)); while(!q.empty()){ int count = q.front().second; v = q.front().first; /*for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ cout << v.cs[i][j] << ' '; }cout << endl; }cout << endl;*/ q.pop(); count++; array<array<char,11>,11> tmp; //tmp.resize(static_cast<unsigned>(x)); /*for(int i=0;i<y;i++){ tmp[i] += v.cs[i]; }*/ tmp = v; if(v[0][0] != 'R'){ DFS(v,v[0][0],'R',0,0); flag = true; for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ if('R' != v[j][i]){ flag = false; break; } } } if(flag){ cout << count << endl; break; } q.push(make_pair(v,count)); /*for(int i=0;i<y;i++){ v[i] = tmp[i]; }*/ v = tmp; } if(v[0][0] != 'G'){ DFS(v,v[0][0],'G',0,0); flag = true; for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ if('G' != v[j][i]){ flag = false; break; } } } if(flag){ cout << count << endl; break; } q.push(make_pair(v,count)); /*for(int i=0;i<y;i++){ v.cs[i] = tmp[i]; }*/ v = tmp; } if(v[0][0] != 'B'){ //char base = v.cs[0][0]; DFS(v,v[0][0],'B',0,0); flag = true; for(int i=0;i<y;i++){ for(int j=0;j<x;j++){ if('B' != v[j][i]){ flag = false; break; } } } if(flag){ cout << count << endl; break; } q.push(make_pair(v,count)); } } } return 0; } ```
#include <bits/stdc++.h> using namespace std; typedef pair<int, string> P; const char rgb[] = "RGB"; const int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1}; int x, y; bool check(string& s) { char start = s[0]; for (char c : s){ if (c != start) return false; } return true; } void change(string& s, int cx, int cy, char from, char to) { if (cx < 0 || x <= cx || cy < 0 || y <= cy || s[cy * x + cx] != from) return; s[cy * x + cx] = to; for (int i = 0; i < 4; i++){ change(s, cx + dx[i], cy + dy[i], from, to); } } int main() { while (scanf("%d %d", &x, &y), x){ char grid[128] = {0}; for (int i = 0; i < x * y; i++){ scanf(" %c", grid + i); } string s(grid); set<string> exist; queue<P> que; for (exist.insert(s), que.push(P(0, s)); ; que.pop()){ P p = que.front(); if (check(p.second)){ printf("%d\n", p.first); break; } for (char c : rgb){ string tmp = p.second; if (tmp[0] != c){ change(tmp, 0, 0, tmp[0], c); if (exist.find(tmp) == exist.end()){ exist.insert(tmp); que.push(P(p.first + 1, tmp)); } } } } } return 0; }
### Prompt Create a solution in CPP for the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <bits/stdc++.h> using namespace std; typedef pair<int, string> P; const char rgb[] = "RGB"; const int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1}; int x, y; bool check(string& s) { char start = s[0]; for (char c : s){ if (c != start) return false; } return true; } void change(string& s, int cx, int cy, char from, char to) { if (cx < 0 || x <= cx || cy < 0 || y <= cy || s[cy * x + cx] != from) return; s[cy * x + cx] = to; for (int i = 0; i < 4; i++){ change(s, cx + dx[i], cy + dy[i], from, to); } } int main() { while (scanf("%d %d", &x, &y), x){ char grid[128] = {0}; for (int i = 0; i < x * y; i++){ scanf(" %c", grid + i); } string s(grid); set<string> exist; queue<P> que; for (exist.insert(s), que.push(P(0, s)); ; que.pop()){ P p = que.front(); if (check(p.second)){ printf("%d\n", p.first); break; } for (char c : rgb){ string tmp = p.second; if (tmp[0] != c){ change(tmp, 0, 0, tmp[0], c); if (exist.find(tmp) == exist.end()){ exist.insert(tmp); que.push(P(p.first + 1, tmp)); } } } } } return 0; } ```
#include<queue> #include<cstdio> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; const int dx[]={1,0,-1,0},dy[]={0,-1,0,1}; int h,w; char B[10][10]; int ub; bool dfs(int t){ bool end=true; rep(i,h) rep(j,w) if(B[i][j]!=B[0][0]) { end=false; break; } if(end) return true; if(t==ub) return false; char B0[10][10],c0=B[0][0]; rep(i,h) rep(j,w) B0[i][j]=B[i][j]; queue< pair<int,int> > Q; rep(ic,3){ char c="RGB"[ic]; if(c==c0) continue; B[0][0]=c; Q.push(make_pair(0,0)); while(!Q.empty()){ int y=Q.front().first,x=Q.front().second; Q.pop(); rep(k,4){ int yy=y+dy[k],xx=x+dx[k]; if(0<=yy && yy<h && 0<=xx && xx<w && B[yy][xx]==c0){ B[yy][xx]=c; Q.push(make_pair(yy,xx)); } } } if(dfs(t+1)) return true; rep(i,h) rep(j,w) B[i][j]=B0[i][j]; } return false; } int main(){ for(;scanf("%d%d",&w,&h),w;){ rep(i,h) rep(j,w) scanf(" %c",B[i]+j); for(ub=0;!dfs(0);ub++); printf("%d\n",ub); } return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include<queue> #include<cstdio> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; const int dx[]={1,0,-1,0},dy[]={0,-1,0,1}; int h,w; char B[10][10]; int ub; bool dfs(int t){ bool end=true; rep(i,h) rep(j,w) if(B[i][j]!=B[0][0]) { end=false; break; } if(end) return true; if(t==ub) return false; char B0[10][10],c0=B[0][0]; rep(i,h) rep(j,w) B0[i][j]=B[i][j]; queue< pair<int,int> > Q; rep(ic,3){ char c="RGB"[ic]; if(c==c0) continue; B[0][0]=c; Q.push(make_pair(0,0)); while(!Q.empty()){ int y=Q.front().first,x=Q.front().second; Q.pop(); rep(k,4){ int yy=y+dy[k],xx=x+dx[k]; if(0<=yy && yy<h && 0<=xx && xx<w && B[yy][xx]==c0){ B[yy][xx]=c; Q.push(make_pair(yy,xx)); } } } if(dfs(t+1)) return true; rep(i,h) rep(j,w) B[i][j]=B0[i][j]; } return false; } int main(){ for(;scanf("%d%d",&w,&h),w;){ rep(i,h) rep(j,w) scanf(" %c",B[i]+j); for(ub=0;!dfs(0);ub++); printf("%d\n",ub); } return 0; } ```
#include<iostream> #include<queue> using namespace std; int x,y; struct grid{ char g[11][11]; int count; }; void DFS(grid& gr,char firstcell,char changecolor,int posx,int posy){ //if(posx < 0 || posx >= x || posy < 0 || posy >= y || gr.g[posx][posy] != firstcell){ if(gr.g[posx][posy] != firstcell){ return; } gr.g[posx][posy] = changecolor; DFS(gr,firstcell,changecolor,posx+1,posy); DFS(gr,firstcell,changecolor,posx-1,posy); DFS(gr,firstcell,changecolor,posx,posy+1); DFS(gr,firstcell,changecolor,posx,posy-1); } int main(){ while(1){ cin >> x >> y; if(x==0 && y==0){ break; } grid start; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ cin >> start.g[j][i]; } } char firstcell = start.g[0][0]; bool flag = true; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ if(start.g[j][i]!=firstcell){ flag = false; break; } } if(!flag){ break; } } if(flag){ cout << 0 << endl; continue; } start.count = 0; queue<grid> q; q.push(start); while(1){ grid BeforeGrid = q.front(); q.pop(); BeforeGrid.count++; grid tmp = BeforeGrid; firstcell = tmp.g[0][0]; if(firstcell != 'R'){ DFS(tmp,firstcell,'R',0,0); flag = true; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ if(tmp.g[j][i]!='R'){ flag = false; break; } } if(!flag){ break; } } if(flag){ cout << tmp.count << endl; break; } q.push(tmp); tmp = BeforeGrid; } if(firstcell != 'G'){ DFS(tmp,firstcell,'G',0,0); flag = true; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ if(tmp.g[j][i]!='G'){ flag = false; break; } } if(!flag){ break; } } if(flag){ cout << tmp.count << endl; break; } q.push(tmp); tmp = BeforeGrid; } if(firstcell != 'B'){ DFS(tmp,firstcell,'B',0,0); flag = true; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ if(tmp.g[j][i]!='B'){ flag = false; break; } } if(!flag){ break; } } if(flag){ cout << tmp.count << endl; break; } q.push(tmp); } } } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include<iostream> #include<queue> using namespace std; int x,y; struct grid{ char g[11][11]; int count; }; void DFS(grid& gr,char firstcell,char changecolor,int posx,int posy){ //if(posx < 0 || posx >= x || posy < 0 || posy >= y || gr.g[posx][posy] != firstcell){ if(gr.g[posx][posy] != firstcell){ return; } gr.g[posx][posy] = changecolor; DFS(gr,firstcell,changecolor,posx+1,posy); DFS(gr,firstcell,changecolor,posx-1,posy); DFS(gr,firstcell,changecolor,posx,posy+1); DFS(gr,firstcell,changecolor,posx,posy-1); } int main(){ while(1){ cin >> x >> y; if(x==0 && y==0){ break; } grid start; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ cin >> start.g[j][i]; } } char firstcell = start.g[0][0]; bool flag = true; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ if(start.g[j][i]!=firstcell){ flag = false; break; } } if(!flag){ break; } } if(flag){ cout << 0 << endl; continue; } start.count = 0; queue<grid> q; q.push(start); while(1){ grid BeforeGrid = q.front(); q.pop(); BeforeGrid.count++; grid tmp = BeforeGrid; firstcell = tmp.g[0][0]; if(firstcell != 'R'){ DFS(tmp,firstcell,'R',0,0); flag = true; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ if(tmp.g[j][i]!='R'){ flag = false; break; } } if(!flag){ break; } } if(flag){ cout << tmp.count << endl; break; } q.push(tmp); tmp = BeforeGrid; } if(firstcell != 'G'){ DFS(tmp,firstcell,'G',0,0); flag = true; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ if(tmp.g[j][i]!='G'){ flag = false; break; } } if(!flag){ break; } } if(flag){ cout << tmp.count << endl; break; } q.push(tmp); tmp = BeforeGrid; } if(firstcell != 'B'){ DFS(tmp,firstcell,'B',0,0); flag = true; for(int i=0; i<y; i++){ for(int j=0; j<x; j++){ if(tmp.g[j][i]!='B'){ flag = false; break; } } if(!flag){ break; } } if(flag){ cout << tmp.count << endl; break; } q.push(tmp); } } } return 0; } ```
#include <queue> #include <vector> #include <iostream> #include <algorithm> using namespace std; int H, W; char c; int main() { ios::sync_with_stdio(false); while (cin >> W >> H, H | W) { vector<vector<int> > a(H, vector<int>(W)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> c; if (c == 'R') a[i][j] = 0; if (c == 'G') a[i][j] = 1; if (c == 'B') a[i][j] = 2; } } vector<vector<pair<int, int> > > comp; vector<vector<bool> > vis(H, vector<bool>(W, false)); vector<int> dir({ 0, 1, 0, -1 }); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (vis[i][j]) continue; comp.push_back(vector<pair<int, int> >({ make_pair(j, i) })); vis[i][j] = true; queue<pair<int, int> > que; que.push(make_pair(j, i)); while (!que.empty()) { pair<int, int> u = que.front(); que.pop(); for (int k = 0; k < 4; k++) { int tx = u.first + dir[k], ty = u.second + dir[k ^ 1]; if (0 <= tx && tx < W && 0 <= ty && ty < H && !vis[ty][tx] && a[u.second][u.first] == a[ty][tx]) { vis[ty][tx] = true; comp.back().push_back(make_pair(tx, ty)); que.push(make_pair(tx, ty)); } } } } } vector<int> col(comp.size()); for (int i = 0; i < comp.size(); i++) { for (pair<int, int> j : comp[i]) { col[i] = a[j.second][j.first]; a[j.second][j.first] = i; } } vector<vector<int> > G(comp.size()); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (j + 1 != W && a[i][j] != a[i][j + 1]) { G[a[i][j]].push_back(a[i][j + 1]); G[a[i][j + 1]].push_back(a[i][j]); } if (i + 1 != H && a[i][j] != a[i + 1][j]) { G[a[i][j]].push_back(a[i + 1][j]); G[a[i + 1][j]].push_back(a[i][j]); } } } for (int i = 0; i < G.size(); i++) { sort(G[i].begin(), G[i].end()); int ptr = unique(G[i].begin(), G[i].end()) - G[i].begin(); G[i].erase(G[i].begin() + ptr, G[i].end()); } for (int i = 0; ; i++) { bool ok = false; for (int j = 0; j < 1 << i; j++) { vector<int> w1(col); for (int k = 0; k < i; k++) { int r = (w1[a[0][0]] + ((j & (1 << k)) ? 2 : 1)) % 3; queue<int> que2; que2.push(a[0][0]); vector<bool> vis2(G.size()); vis2[a[0][0]] = true; while (!que2.empty()) { int u = que2.front(); que2.pop(); for (int l : G[u]) { if (!vis2[l] && w1[u] == w1[l]) { vis2[l] = true; que2.push(l); } } } for (int l = 0; l < G.size(); l++) { if (vis2[l]) w1[l] = r; } } bool flag = true; for (int k = 1; k < G.size(); k++) { if (w1[0] != w1[k]) { flag = false; break; } } if (flag) { ok = true; break; } } if (ok) { cout << i << endl; break; } } } return 0; }
### Prompt Please create a solution in Cpp to the following problem: The tablet interface technology, which can be operated by touching the screen with a finger, has also been applied in the field of games, and various types of games with new operability have been created. The game currently being developed by AZ is one of them. The requirements for this software (game) are as follows. * The screen has a 2D grid of X columns x Y rows. * The cells of the grid are painted in one of three colors: red (R), green (G), and brown (B). * There are buttons R, G, B to change the color of the cell, and when this button is pressed, the upper left (0,0) cell will be changed to that color. * When the color of the top left cell is changed, all adjacent cells painted in the same color as the original color of that cell will change to the specified color. That is, all cells that are connected by cells with the same original color will be changed to the specified color. It is a game that fills the grid in this way and finally makes the entire grid one color. A high score will be given if you solve it with a few steps. As a programmer at AZ, you have decided to develop a part of the logic of this game. First of all, in order to calculate this maximum score, I decided to make a program to find out how many times the selection button should be operated to make the grid one color if the game is solved in the shortest time. <image> input Given multiple datasets. The end of the input is indicated by two zeros. Each dataset is given in the following format: X Y c1,1 c2,1 ... cX,1 c1,2 c2,2 ... cX,2 :: c1, Y c2, Y ... cX, Y The first row gives the grid columns and row sizes X, Y (2 ≤ X, Y ≤ 10). The following Y-row is given the letters ci, j, which represent the color of the cells in the i-th and j-th rows, separated by blanks. output Outputs the minimum number of button operations on one line for each dataset. Example Input 3 3 R G B G G G G B B 2 4 R G G G R B B R 4 3 G G B R G R R G B G G R 0 0 Output 2 4 4 ### Response ```cpp #include <queue> #include <vector> #include <iostream> #include <algorithm> using namespace std; int H, W; char c; int main() { ios::sync_with_stdio(false); while (cin >> W >> H, H | W) { vector<vector<int> > a(H, vector<int>(W)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> c; if (c == 'R') a[i][j] = 0; if (c == 'G') a[i][j] = 1; if (c == 'B') a[i][j] = 2; } } vector<vector<pair<int, int> > > comp; vector<vector<bool> > vis(H, vector<bool>(W, false)); vector<int> dir({ 0, 1, 0, -1 }); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (vis[i][j]) continue; comp.push_back(vector<pair<int, int> >({ make_pair(j, i) })); vis[i][j] = true; queue<pair<int, int> > que; que.push(make_pair(j, i)); while (!que.empty()) { pair<int, int> u = que.front(); que.pop(); for (int k = 0; k < 4; k++) { int tx = u.first + dir[k], ty = u.second + dir[k ^ 1]; if (0 <= tx && tx < W && 0 <= ty && ty < H && !vis[ty][tx] && a[u.second][u.first] == a[ty][tx]) { vis[ty][tx] = true; comp.back().push_back(make_pair(tx, ty)); que.push(make_pair(tx, ty)); } } } } } vector<int> col(comp.size()); for (int i = 0; i < comp.size(); i++) { for (pair<int, int> j : comp[i]) { col[i] = a[j.second][j.first]; a[j.second][j.first] = i; } } vector<vector<int> > G(comp.size()); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (j + 1 != W && a[i][j] != a[i][j + 1]) { G[a[i][j]].push_back(a[i][j + 1]); G[a[i][j + 1]].push_back(a[i][j]); } if (i + 1 != H && a[i][j] != a[i + 1][j]) { G[a[i][j]].push_back(a[i + 1][j]); G[a[i + 1][j]].push_back(a[i][j]); } } } for (int i = 0; i < G.size(); i++) { sort(G[i].begin(), G[i].end()); int ptr = unique(G[i].begin(), G[i].end()) - G[i].begin(); G[i].erase(G[i].begin() + ptr, G[i].end()); } for (int i = 0; ; i++) { bool ok = false; for (int j = 0; j < 1 << i; j++) { vector<int> w1(col); for (int k = 0; k < i; k++) { int r = (w1[a[0][0]] + ((j & (1 << k)) ? 2 : 1)) % 3; queue<int> que2; que2.push(a[0][0]); vector<bool> vis2(G.size()); vis2[a[0][0]] = true; while (!que2.empty()) { int u = que2.front(); que2.pop(); for (int l : G[u]) { if (!vis2[l] && w1[u] == w1[l]) { vis2[l] = true; que2.push(l); } } } for (int l = 0; l < G.size(); l++) { if (vis2[l]) w1[l] = r; } } bool flag = true; for (int k = 1; k < G.size(); k++) { if (w1[0] != w1[k]) { flag = false; break; } } if (flag) { ok = true; break; } } if (ok) { cout << i << endl; break; } } } return 0; } ```
#include<bits/stdc++.h> using namespace std; int main(){ int n,m; cin >> n; while(n != 0){ char a[n+1],b[n+1]; for(int i = 0;i < n;i++) cin >> a[i] >> b[i]; cin >> m; char ans[m+1]; for(int i = 0;i < m;i++){ cin >> ans[i]; for(int j = 0;j < n;j++){ if(ans[i] == a[j]){ ans[i] = b[j]; break; } } } ans[m]='\0'; cout << ans << endl; cin >> n; } return (0); }
### Prompt Please provide a CPP coded solution to the problem described below: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include<bits/stdc++.h> using namespace std; int main(){ int n,m; cin >> n; while(n != 0){ char a[n+1],b[n+1]; for(int i = 0;i < n;i++) cin >> a[i] >> b[i]; cin >> m; char ans[m+1]; for(int i = 0;i < m;i++){ cin >> ans[i]; for(int j = 0;j < n;j++){ if(ans[i] == a[j]){ ans[i] = b[j]; break; } } } ans[m]='\0'; cout << ans << endl; cin >> n; } return (0); } ```
#include <iostream> #include <string> #define rep(i, a, b) for(int i = a; i < b; i ++) using namespace std; int main() { int n, m; char A[100], B[100], C; while(cin >> n, n) { rep(i, 0, 100) { A[i] = 0; B[i] = 0; } rep(i, 0, n) { cin >> A[i] >> B[i]; } cin >> m; rep(i, 0, m) { cin >> C; rep(j, 0, n) { if(C == A[j]) { C = B[j]; break; } } cout << C; } cout << endl; } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include <iostream> #include <string> #define rep(i, a, b) for(int i = a; i < b; i ++) using namespace std; int main() { int n, m; char A[100], B[100], C; while(cin >> n, n) { rep(i, 0, 100) { A[i] = 0; B[i] = 0; } rep(i, 0, n) { cin >> A[i] >> B[i]; } cin >> m; rep(i, 0, m) { cin >> C; rep(j, 0, n) { if(C == A[j]) { C = B[j]; break; } } cout << C; } cout << endl; } return 0; } ```
#include <iostream> #include <map> #include <string> using namespace std; int main() { int n, m; while (cin >> n, n) { char a, b; string str; map<char, char> to; for (int i = 0; i < n; ++i) { cin >> a >> b; to[a] = b; } cin >> m; for (int i = 0; i < m; ++i) { cin >> a; if (to[a]) a = to[a]; str += a; } cout << str << endl; } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include <iostream> #include <map> #include <string> using namespace std; int main() { int n, m; while (cin >> n, n) { char a, b; string str; map<char, char> to; for (int i = 0; i < n; ++i) { cin >> a >> b; to[a] = b; } cin >> m; for (int i = 0; i < m; ++i) { cin >> a; if (to[a]) a = to[a]; str += a; } cout << str << endl; } return 0; } ```
#import<iostream> using namespace std; int main(){ int n, m; char a, b; while(cin>>n, n){ int d[256] = {}; for(int i=0;i<n;i++){ cin >> a >> b; d[a] = b-a; } for(cin>>n;n--;putchar(a+d[a])) cin>>a; puts(""); } }
### Prompt Please formulate a Cpp solution to the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #import<iostream> using namespace std; int main(){ int n, m; char a, b; while(cin>>n, n){ int d[256] = {}; for(int i=0;i<n;i++){ cin >> a >> b; d[a] = b-a; } for(cin>>n;n--;putchar(a+d[a])) cin>>a; puts(""); } } ```
#include <iostream> using namespace std; int main(){ while(1){ int n,m; cin>>n; if(n==0) break; char map[n][2]; for(int i=0;i<n;i++){ cin>>map[i][0]>>map[i][1]; } cin>>m; char result[m+1]; for(int i=0;i<m;i++){ cin>>result[i]; for(int j=0;j<n;j++){ if(result[i]==map[j][0]){ result[i]=map[j][1]; break; } } } result[m]=0; cout<<result<<endl; } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include <iostream> using namespace std; int main(){ while(1){ int n,m; cin>>n; if(n==0) break; char map[n][2]; for(int i=0;i<n;i++){ cin>>map[i][0]>>map[i][1]; } cin>>m; char result[m+1]; for(int i=0;i<m;i++){ cin>>result[i]; for(int j=0;j<n;j++){ if(result[i]==map[j][0]){ result[i]=map[j][1]; break; } } } result[m]=0; cout<<result<<endl; } return 0; } ```
#include <cstdio> #include <map> using namespace std; int main() { int n; while (scanf("%d", &n), n) { map<char, char> table; for (int i = 0; i < n; ++i) { char from, to; scanf(" %c %c", &from, &to); table[from] = to; } int m; scanf("%d", &m); for (int i = 0; i < m; ++i){ char ch; scanf(" %c", &ch); putchar(table.count(ch) ? table[ch] : ch); } puts(""); } }
### Prompt Generate a CPP solution to the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include <cstdio> #include <map> using namespace std; int main() { int n; while (scanf("%d", &n), n) { map<char, char> table; for (int i = 0; i < n; ++i) { char from, to; scanf(" %c %c", &from, &to); table[from] = to; } int m; scanf("%d", &m); for (int i = 0; i < m; ++i){ char ch; scanf(" %c", &ch); putchar(table.count(ch) ? table[ch] : ch); } puts(""); } } ```
#include<iostream> using namespace std; char T[25000][2]; char S; int main(){ int n,m; while(true){ cin>>n; if(n==0){ break; } for(int i=0;i<n;i++){ cin>>T[i][0]>>T[i][1]; } cin>>m; for(int i=0;i<m;i++){ cin>>S; for(int j=0;j<n;j++){ if(T[j][0]==S){ cout<<T[j][1]; goto Exit; } } cout<<S; Exit:; } cout<<endl; } }
### Prompt Please formulate a Cpp solution to the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include<iostream> using namespace std; char T[25000][2]; char S; int main(){ int n,m; while(true){ cin>>n; if(n==0){ break; } for(int i=0;i<n;i++){ cin>>T[i][0]>>T[i][1]; } cin>>m; for(int i=0;i<m;i++){ cin>>S; for(int j=0;j<n;j++){ if(T[j][0]==S){ cout<<T[j][1]; goto Exit; } } cout<<S; Exit:; } cout<<endl; } } ```
#include <cstdio> int main() { int n; while (scanf("%d", &n) != EOF && n) { char C[200]; for (auto i=0; i<200; i++) { C[i] = (char)i; } for (auto i=0; i<n; i++) { char a, b; scanf(" %c %c", &a, &b); C[(int)a] = b; } int m; scanf("%d", &m); for (auto j=0; j<m; j++) { char d; scanf(" %c", &d); printf("%c", C[(int)d]); } printf("\n"); } }
### Prompt Construct a cpp code solution to the problem outlined: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include <cstdio> int main() { int n; while (scanf("%d", &n) != EOF && n) { char C[200]; for (auto i=0; i<200; i++) { C[i] = (char)i; } for (auto i=0; i<n; i++) { char a, b; scanf(" %c %c", &a, &b); C[(int)a] = b; } int m; scanf("%d", &m); for (auto j=0; j<m; j++) { char d; scanf(" %c", &d); printf("%c", C[(int)d]); } printf("\n"); } } ```
#include<bits/stdc++.h> using namespace std; int main(){ int a; while(cin>>a,a){ map<string,string>s; for(int b=0;b<a;b++){ string c,k; cin>>c>>k; s.insert(pair<string,string>(c,k)); } int d; cin>>d; string r=""; for(int e=0;e<d;e++){ string t; cin>>t; if(s[t].empty()==true)s[t]=t;; r+=s[t]; } cout<<r<<endl; } }
### Prompt Please create a solution in cpp to the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include<bits/stdc++.h> using namespace std; int main(){ int a; while(cin>>a,a){ map<string,string>s; for(int b=0;b<a;b++){ string c,k; cin>>c>>k; s.insert(pair<string,string>(c,k)); } int d; cin>>d; string r=""; for(int e=0;e<d;e++){ string t; cin>>t; if(s[t].empty()==true)s[t]=t;; r+=s[t]; } cout<<r<<endl; } } ```
#include<iostream> using namespace std; int main() { int n, m; char data[100][2], str[100000]; while (cin >> n && n) { for (int i = 0; i < n; ++i) cin >> data[i][0] >> data[i][1]; cin >> m; for (int i = 0; i < m; ++i) { cin >> str[i]; for (int j = 0; j < n; ++j) { if (str[i] == data[j][0]) { str[i] = data[j][1]; break; } } } for (int i = 0; i < m; ++i) cout << str[i]; cout << endl; } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include<iostream> using namespace std; int main() { int n, m; char data[100][2], str[100000]; while (cin >> n && n) { for (int i = 0; i < n; ++i) cin >> data[i][0] >> data[i][1]; cin >> m; for (int i = 0; i < m; ++i) { cin >> str[i]; for (int j = 0; j < n; ++j) { if (str[i] == data[j][0]) { str[i] = data[j][1]; break; } } } for (int i = 0; i < m; ++i) cout << str[i]; cout << endl; } return 0; } ```
#include <iostream> #include <map> using namespace std; int main() { int n,m; char c; pair<char,char> data[10000]; while(true){ cin>>n; if(!n)break; for(int i=0;i<n;i++)cin>>data[i].first>>data[i].second; cin>>m; for(int i=0;i<m;i++){ cin>>c; for(int j=0;j<n;j++)if(data[j].first==c){c=data[j].second;break;} cout<<c; } cout<<endl; } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include <iostream> #include <map> using namespace std; int main() { int n,m; char c; pair<char,char> data[10000]; while(true){ cin>>n; if(!n)break; for(int i=0;i<n;i++)cin>>data[i].first>>data[i].second; cin>>m; for(int i=0;i<m;i++){ cin>>c; for(int j=0;j<n;j++)if(data[j].first==c){c=data[j].second;break;} cout<<c; } cout<<endl; } return 0; } ```
#include <iostream> #include <string> using namespace std; int main(void){ int n, m; cin >> n; while(n){ char henkan[100][2]; for(int i = 0; i < n; i++) cin >> henkan[i][0] >> henkan[i][1]; cin >> m; char data; string moji; for(int i = 0; i < m; i++){ cin >> data; for(int j = 0; j < n; j++){ if(data == henkan[j][0]){ data = henkan[j][1]; break; } } moji += data; } cout << moji << endl; cin >> n; } }
### Prompt Please formulate a cpp solution to the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include <iostream> #include <string> using namespace std; int main(void){ int n, m; cin >> n; while(n){ char henkan[100][2]; for(int i = 0; i < n; i++) cin >> henkan[i][0] >> henkan[i][1]; cin >> m; char data; string moji; for(int i = 0; i < m; i++){ cin >> data; for(int j = 0; j < n; j++){ if(data == henkan[j][0]){ data = henkan[j][1]; break; } } moji += data; } cout << moji << endl; cin >> n; } } ```
#include<iostream> #include<map> using namespace std; int main(){ int n; map<char,char>mp; while(cin >> n,n){ for(int i=0;i<n;i++){ char a, b; cin >> a >> b; mp[a] = b; } int m; cin >> m; for(int i=0;i<m;i++){ char a; cin >> a; if(mp.find(a)!=mp.end()) cout << mp[a]; else cout << a; } cout << endl; mp.clear(); } }
### Prompt Your challenge is to write a CPP solution to the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include<iostream> #include<map> using namespace std; int main(){ int n; map<char,char>mp; while(cin >> n,n){ for(int i=0;i<n;i++){ char a, b; cin >> a >> b; mp[a] = b; } int m; cin >> m; for(int i=0;i<m;i++){ char a; cin >> a; if(mp.find(a)!=mp.end()) cout << mp[a]; else cout << a; } cout << endl; mp.clear(); } } ```
#include<iostream> #include<map> #include<string> #include<cstdio> using namespace std; int main(){ long int n; string x,y,z; while(cin >> n, n){ long int m; map < string,string > a; for(int i=0;i<n;i++){ cin >> x >> y; a[x] = y; } cin >> m; for(int i=0;i<m;i++){ cin >> z; if(a[z].empty() == false) cout << a[z]; else cout << z; } cout << endl; } }
### Prompt In cpp, your task is to solve the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include<iostream> #include<map> #include<string> #include<cstdio> using namespace std; int main(){ long int n; string x,y,z; while(cin >> n, n){ long int m; map < string,string > a; for(int i=0;i<n;i++){ cin >> x >> y; a[x] = y; } cin >> m; for(int i=0;i<m;i++){ cin >> z; if(a[z].empty() == false) cout << a[z]; else cout << z; } cout << endl; } } ```
#include <iostream> #include <vector> using namespace std; int main(){ int a,b,i,j; while(1){ cin>>a; if(a==0) break; vector<char>from(a); vector<char>to(a); for(i=0;i<a;i++){ cin>>from[i]>>to[i]; } cin>>b; vector<char>v(b); for(i=0;i<b;i++){ cin>>v[i]; for(j=0;j<a;j++){ if(v[i]==from[j]){ v[i]=to[j]; break; } } } for(i=0;i<b;i++){ cout<<v[i]; } cout<<"\n"; } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include <iostream> #include <vector> using namespace std; int main(){ int a,b,i,j; while(1){ cin>>a; if(a==0) break; vector<char>from(a); vector<char>to(a); for(i=0;i<a;i++){ cin>>from[i]>>to[i]; } cin>>b; vector<char>v(b); for(i=0;i<b;i++){ cin>>v[i]; for(j=0;j<a;j++){ if(v[i]==from[j]){ v[i]=to[j]; break; } } } for(i=0;i<b;i++){ cout<<v[i]; } cout<<"\n"; } return 0; } ```
#include<stdio.h> int main(void) { int i,j,n,m; char s[100],t[100]; char a; scanf("%d",&n); while (n!=0) { for (i=0;i<n;i++) { scanf(" %c %c",&s[i],&t[i]); } scanf("%d",&m); for (i=0;i<m;i++) { scanf(" %c",&a); for (j=0;j<n;j++) { if(a == s[j]) { a=t[j]; break; } } printf("%c",a); } printf("\n"); scanf("%d",&n); } return 0; }
### Prompt Develop a solution in cpp to the problem described below: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include<stdio.h> int main(void) { int i,j,n,m; char s[100],t[100]; char a; scanf("%d",&n); while (n!=0) { for (i=0;i<n;i++) { scanf(" %c %c",&s[i],&t[i]); } scanf("%d",&m); for (i=0;i<m;i++) { scanf(" %c",&a); for (j=0;j<n;j++) { if(a == s[j]) { a=t[j]; break; } } printf("%c",a); } printf("\n"); scanf("%d",&n); } return 0; } ```
#include <iostream> using namespace std; char conv[128]; void make() { for(int i = 0; i < 128; ++i) conv[i] = i; } int main() { int n, m; while((cin >> n) && n) { make(); for(int i = 0; i < n; ++i) { char a, b; cin >> a >> b; conv[a] = b; } cin >> m; for(int i = 0; i < m; ++i) { char c; cin >> c; cout << conv[c]; } cout << endl; } }
### Prompt Please provide a CPP coded solution to the problem described below: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include <iostream> using namespace std; char conv[128]; void make() { for(int i = 0; i < 128; ++i) conv[i] = i; } int main() { int n, m; while((cin >> n) && n) { make(); for(int i = 0; i < n; ++i) { char a, b; cin >> a >> b; conv[a] = b; } cin >> m; for(int i = 0; i < m; ++i) { char c; cin >> c; cout << conv[c]; } cout << endl; } } ```
#include<bits/stdc++.h> using namespace std; int N; void solve(){ map<char,char>M; while(N--){ char from,to; scanf(" %c %c",&from,&to); M[from]=to; } int K; scanf("%d",&K); while(K--){ char c; scanf(" %c",&c); if(M.find(c)!=M.end())printf("%c",M[c]); else printf("%c",c); } puts(""); } int main(){ while(scanf("%d",&N),N)solve(); return 0; }
### Prompt Create a solution in cpp for the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include<bits/stdc++.h> using namespace std; int N; void solve(){ map<char,char>M; while(N--){ char from,to; scanf(" %c %c",&from,&to); M[from]=to; } int K; scanf("%d",&K); while(K--){ char c; scanf(" %c",&c); if(M.find(c)!=M.end())printf("%c",M[c]); else printf("%c",c); } puts(""); } int main(){ while(scanf("%d",&N),N)solve(); return 0; } ```
#include<stdio.h> #include<string.h> int n,m; int i,j; char a,b,c; char list[1000]; int main(){ while(1){ scanf("%d",&n); if(n==0)return 0; for(i=0;i<1000;i++)list[i]=i; for(i=0;i<n;i++){ scanf(" %c %c",&a,&b); list[a]=b; } scanf("%d",&m); for(i=0;i<m;i++){ scanf(" %c",&c); printf("%c",list[c]); } printf("\n"); } }
### Prompt Construct a CPP code solution to the problem outlined: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include<stdio.h> #include<string.h> int n,m; int i,j; char a,b,c; char list[1000]; int main(){ while(1){ scanf("%d",&n); if(n==0)return 0; for(i=0;i<1000;i++)list[i]=i; for(i=0;i<n;i++){ scanf(" %c %c",&a,&b); list[a]=b; } scanf("%d",&m); for(i=0;i<m;i++){ scanf(" %c",&c); printf("%c",list[c]); } printf("\n"); } } ```
#include <iostream> #include <string> using namespace std; int main() { int n; char t[100], a[100]; while (cin >> n, n){ for (int i = 0; i < n; i++){ cin >> t[i] >> a[i]; } int m; string s; cin >> m; for (int i = 0; i < m; i++){ char c; cin >> c; for (int j = 0; j < n; j++){ if (c == t[j]){ c = a[j]; break; } } s += c; } cout << s << endl; } return (0); }
### Prompt Your challenge is to write a Cpp solution to the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include <iostream> #include <string> using namespace std; int main() { int n; char t[100], a[100]; while (cin >> n, n){ for (int i = 0; i < n; i++){ cin >> t[i] >> a[i]; } int m; string s; cin >> m; for (int i = 0; i < m; i++){ char c; cin >> c; for (int j = 0; j < n; j++){ if (c == t[j]){ c = a[j]; break; } } s += c; } cout << s << endl; } return (0); } ```
#include<stdio.h> #include<map> int main() { int n; char s[8]; while(scanf("%d",&n),n) { std::map<char,char>m; fgets(s,8,stdin); while(n--) fgets(s,8,stdin),m[s[0]]=s[2]; scanf("%d",&n); while(n--) scanf("%s",s),putchar(m.find(s[0])==m.end()?s[0]:m[s[0]]); puts(""); } return 0; }
### Prompt In CPP, your task is to solve the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include<stdio.h> #include<map> int main() { int n; char s[8]; while(scanf("%d",&n),n) { std::map<char,char>m; fgets(s,8,stdin); while(n--) fgets(s,8,stdin),m[s[0]]=s[2]; scanf("%d",&n); while(n--) scanf("%s",s),putchar(m.find(s[0])==m.end()?s[0]:m[s[0]]); puts(""); } return 0; } ```
#include<stdio.h> int main(){ int c[256]; int a; while(1){ scanf("%d",&a); if(a==0)return 0; for(int i=0;i<256;i++)c[i]=i; getchar(); getchar(); for(int i=0;i<a;i++){ char b,d; b=getchar(); getchar(); d=getchar(); getchar(); getchar(); c[b]=d; } int e; scanf("%d",&e); getchar(); getchar(); char f; for(int i=0;i<e;i++){ f=getchar(); getchar(); getchar(); putchar(c[(int)f]); } printf("\n"); } }
### Prompt Generate a CPP solution to the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include<stdio.h> int main(){ int c[256]; int a; while(1){ scanf("%d",&a); if(a==0)return 0; for(int i=0;i<256;i++)c[i]=i; getchar(); getchar(); for(int i=0;i<a;i++){ char b,d; b=getchar(); getchar(); d=getchar(); getchar(); getchar(); c[b]=d; } int e; scanf("%d",&e); getchar(); getchar(); char f; for(int i=0;i<e;i++){ f=getchar(); getchar(); getchar(); putchar(c[(int)f]); } printf("\n"); } } ```
#include <stdio.h> int main(void) { char table[100000][2]; long int n,m,i,c; char buf; while(scanf("%ld",&n)){ if(n == 0)break; for(i = 0; n > i; i++){ getchar(); scanf("%c",&table[i][0]); getchar(); scanf("%c",&table[i][1]); } scanf("%ld",&m); for(i = 0; m > i; i++){ getchar(); scanf("%c",&buf); for(c = 0; n > c; c++)if(buf == table[c][0]) {buf = table[c][1];break;} printf("%c",buf); } printf("\n"); } return 0; }
### Prompt In cpp, your task is to solve the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include <stdio.h> int main(void) { char table[100000][2]; long int n,m,i,c; char buf; while(scanf("%ld",&n)){ if(n == 0)break; for(i = 0; n > i; i++){ getchar(); scanf("%c",&table[i][0]); getchar(); scanf("%c",&table[i][1]); } scanf("%ld",&m); for(i = 0; m > i; i++){ getchar(); scanf("%c",&buf); for(c = 0; n > c; c++)if(buf == table[c][0]) {buf = table[c][1];break;} printf("%c",buf); } printf("\n"); } return 0; } ```
#include <iostream> #include <cstring> using namespace std; int main() { int n,m; int henkan[256]; while(cin>>n,n){ memset(henkan,0,sizeof(henkan)); while(n--){ char a,b; cin>>a>>b; henkan[(int)a] = b; } cin>>m; while(m--){ char a; cin>>a; printf("%c",henkan[(int)a]?:a); } puts(""); } return 0; }
### Prompt In Cpp, your task is to solve the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include <iostream> #include <cstring> using namespace std; int main() { int n,m; int henkan[256]; while(cin>>n,n){ memset(henkan,0,sizeof(henkan)); while(n--){ char a,b; cin>>a>>b; henkan[(int)a] = b; } cin>>m; while(m--){ char a; cin>>a; printf("%c",henkan[(int)a]?:a); } puts(""); } return 0; } ```
#include <cstdio> #include <cstring> int n, m; char e[261], f, g; int main() { while(scanf("%d\n", &n), n) { for(int i = 0; i < 256; i++) e[i] = i; for(int i = 0; i < n; i++) scanf("%c %c\n", &f, &g), e[f] = g; scanf("%d\n", &m); for(int i = 0; i < m; i++) scanf("%c\n", &f), printf("%c", e[f]); printf("\n"); } }
### Prompt Generate a Cpp solution to the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include <cstdio> #include <cstring> int n, m; char e[261], f, g; int main() { while(scanf("%d\n", &n), n) { for(int i = 0; i < 256; i++) e[i] = i; for(int i = 0; i < n; i++) scanf("%c %c\n", &f, &g), e[f] = g; scanf("%d\n", &m); for(int i = 0; i < m; i++) scanf("%c\n", &f), printf("%c", e[f]); printf("\n"); } } ```
#include <bits/stdc++.h> using namespace std; int n, q; int main() { while(cin >> n, n) { map<char, char> conversion; for(int i = 0; i < 128; i++) conversion[i] = i; string c, d; while(n--) cin >> c >> d, conversion[c[0]] = d[0]; cin >> q; while(q--) cin >> c, cout << conversion[c[0]]; cout << endl; } }
### Prompt Please provide a Cpp coded solution to the problem described below: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, q; int main() { while(cin >> n, n) { map<char, char> conversion; for(int i = 0; i < 128; i++) conversion[i] = i; string c, d; while(n--) cin >> c >> d, conversion[c[0]] = d[0]; cin >> q; while(q--) cin >> c, cout << conversion[c[0]]; cout << endl; } } ```
#include <iostream> #include <algorithm> #include <cassert> #include <cctype> #include <cstdio> #include <math.h> #include <map> #include <queue> #include <string> using namespace std; int n,m; char a,b; int main(){ while(cin>>n){ if(n==0)return 0; map<char,char> table; for(int i=0;i<n;i++){ cin>>a>>b; table[a]=b; } cin>>m; for(int i=0;i<m;i++){ cin>>a; if(table[a]!=0)cout<<table[a]; else cout<<a; } cout<<endl; } }
### Prompt In cpp, your task is to solve the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include <iostream> #include <algorithm> #include <cassert> #include <cctype> #include <cstdio> #include <math.h> #include <map> #include <queue> #include <string> using namespace std; int n,m; char a,b; int main(){ while(cin>>n){ if(n==0)return 0; map<char,char> table; for(int i=0;i<n;i++){ cin>>a>>b; table[a]=b; } cin>>m; for(int i=0;i<m;i++){ cin>>a; if(table[a]!=0)cout<<table[a]; else cout<<a; } cout<<endl; } } ```
#include<iostream> using namespace std; int main(){ int time; char word; while(cin>>time&&time!=0){ char henkan[128]={0}; char hena,henb; for(int i=0;i<time;++i){ cin>>hena>>henb; henkan[hena]=henb; } cin>>time; for(int i=0;i<time;++i){ cin>>word; if(henkan[word]!=0){ cout<<(char)henkan[word]; }else{ cout<<word; } } cout<<endl; } return 0; }
### Prompt Please create a solution in Cpp to the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include<iostream> using namespace std; int main(){ int time; char word; while(cin>>time&&time!=0){ char henkan[128]={0}; char hena,henb; for(int i=0;i<time;++i){ cin>>hena>>henb; henkan[hena]=henb; } cin>>time; for(int i=0;i<time;++i){ cin>>word; if(henkan[word]!=0){ cout<<(char)henkan[word]; }else{ cout<<word; } } cout<<endl; } return 0; } ```
#include<iostream> using namespace std; int main(){ while(1){ char d; char ans[101000]; char f[1010000],g[101000]; int n; cin>>n; if(n==0)break; for(int i=0;i<n;i++){ cin>>d; f[i]=d; cin>>d; g[i]=d; } int s; cin>>s; for(int i=0;i<s;i++){ cin>>d; ans[i]=d; for(int j=0;j<n;j++){ if(ans[i]==f[j]){ ans[i]=g[j]; break; } } } for(int i=0;i<s;i++){ cout<<ans[i]; } cout<<endl; } }
### Prompt Develop a solution in cpp to the problem described below: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include<iostream> using namespace std; int main(){ while(1){ char d; char ans[101000]; char f[1010000],g[101000]; int n; cin>>n; if(n==0)break; for(int i=0;i<n;i++){ cin>>d; f[i]=d; cin>>d; g[i]=d; } int s; cin>>s; for(int i=0;i<s;i++){ cin>>d; ans[i]=d; for(int j=0;j<n;j++){ if(ans[i]==f[j]){ ans[i]=g[j]; break; } } } for(int i=0;i<s;i++){ cout<<ans[i]; } cout<<endl; } } ```
#include<bits/stdc++.h> using namespace std; int main(){ int n; map<char,char> s; while(1){ cin>>n; if(n==0) break; s.clear(); for(int i=0;i<n;i++){ char a,b; cin>>a>>b; s[a]=b; } int m; cin>>m; for(int i=0;i<m;i++){ char a; cin>>a; if(s.count(a)==0){ cout<<a; }else{ cout<<s[a]; } } putchar('\n'); } }
### Prompt Your task is to create a CPP solution to the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include<bits/stdc++.h> using namespace std; int main(){ int n; map<char,char> s; while(1){ cin>>n; if(n==0) break; s.clear(); for(int i=0;i<n;i++){ char a,b; cin>>a>>b; s[a]=b; } int m; cin>>m; for(int i=0;i<m;i++){ char a; cin>>a; if(s.count(a)==0){ cout<<a; }else{ cout<<s[a]; } } putchar('\n'); } } ```
#include <iostream> using namespace std; int main(){ int n; cin >> n; do{ char a[256],b[256]; for(int i=0; i<n; i++){ cin >> a[i] >> b[i]; } int num; char s; cin >> num; for(int i=0; i<num; i++){ cin >> s; for(int j=0; j<n; j++){ if(s==a[j]){ s=b[j]; break; } } cout << s; } cout << endl; cin >> n; }while(n!=0); }
### Prompt Generate a CPP solution to the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include <iostream> using namespace std; int main(){ int n; cin >> n; do{ char a[256],b[256]; for(int i=0; i<n; i++){ cin >> a[i] >> b[i]; } int num; char s; cin >> num; for(int i=0; i<num; i++){ cin >> s; for(int j=0; j<n; j++){ if(s==a[j]){ s=b[j]; break; } } cout << s; } cout << endl; cin >> n; }while(n!=0); } ```
#include<bits/stdc++.h> using namespace std; int main(){ map<char,char>maps; int n,m; char a,b; while(1){ for(int i=0;i<26;i++){ maps['A'+i]='A'+i; maps['a'+i]='a'+i; } for(int i=0;i<10;i++){ maps['0'+i]='0'+i; } cin>>n; if(!n)break; while(n--){ cin>>a>>b; maps[a]=b; } cin>>m; char str[m]; for(int i=0;i<m;i++){ cin>>a; str[i]=maps[a]; } for(int i=0;i<m;i++){ cout<<str[i]; } cout<<endl; } }
### Prompt In cpp, your task is to solve the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include<bits/stdc++.h> using namespace std; int main(){ map<char,char>maps; int n,m; char a,b; while(1){ for(int i=0;i<26;i++){ maps['A'+i]='A'+i; maps['a'+i]='a'+i; } for(int i=0;i<10;i++){ maps['0'+i]='0'+i; } cin>>n; if(!n)break; while(n--){ cin>>a>>b; maps[a]=b; } cin>>m; char str[m]; for(int i=0;i<m;i++){ cin>>a; str[i]=maps[a]; } for(int i=0;i<m;i++){ cout<<str[i]; } cout<<endl; } } ```
#include <iostream> #include <algorithm> using namespace std; int main(){ int n; while(cin >> n, n){ char t[128]; fill(t, t + 128, -1); for(int i = 0; i < n; i++){ char a, b; cin >> a >> b; t[a] = b; } int m; cin >> m; for(int i = 0; i < m; i++){ char ch; cin >> ch; cout << (t[ch] != -1 ? t[ch] : ch); } cout << endl; } }
### Prompt Create a solution in CPP for the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include <iostream> #include <algorithm> using namespace std; int main(){ int n; while(cin >> n, n){ char t[128]; fill(t, t + 128, -1); for(int i = 0; i < n; i++){ char a, b; cin >> a >> b; t[a] = b; } int m; cin >> m; for(int i = 0; i < m; i++){ char ch; cin >> ch; cout << (t[ch] != -1 ? t[ch] : ch); } cout << endl; } } ```
#include<iostream> #include<string> using namespace std; int main(){ int n,m; while(cin >> n&&n!=0){ char conv[n][2]; for(int i=0;i<n;i++){ cin >> conv[i][0] >> conv[i][1]; } cin >> m; char inp[m]; for(int i=0;i<m;i++){ cin >> inp[i]; for(int j=0;j<n;j++){ if(inp[i]==conv[j][0]){ inp[i]=conv[j][1]; break; } } } for(int i=0;i<m;i++){ cout << inp[i]; } cout << endl; } }
### Prompt Generate a cpp solution to the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include<iostream> #include<string> using namespace std; int main(){ int n,m; while(cin >> n&&n!=0){ char conv[n][2]; for(int i=0;i<n;i++){ cin >> conv[i][0] >> conv[i][1]; } cin >> m; char inp[m]; for(int i=0;i<m;i++){ cin >> inp[i]; for(int j=0;j<n;j++){ if(inp[i]==conv[j][0]){ inp[i]=conv[j][1]; break; } } } for(int i=0;i<m;i++){ cout << inp[i]; } cout << endl; } } ```
#include<iostream> #include<map> using namespace std; int main(){ int n; char a,b; map<char,char> list; map<char,char>::iterator it; while(1){ cin>>n; if(n==0)break; for(int i=0;i<n;i++){ cin>>a>>b; list.insert(pair<char,char>(a,b)); } cin>>n; for(int i=0;i<n;i++){ cin>>a; it=list.find(a); if(it==list.end()){ cout<<a; }else{ cout<<(*it).second; } } cout<<endl; list.clear(); } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include<iostream> #include<map> using namespace std; int main(){ int n; char a,b; map<char,char> list; map<char,char>::iterator it; while(1){ cin>>n; if(n==0)break; for(int i=0;i<n;i++){ cin>>a>>b; list.insert(pair<char,char>(a,b)); } cin>>n; for(int i=0;i<n;i++){ cin>>a; it=list.find(a); if(it==list.end()){ cout<<a; }else{ cout<<(*it).second; } } cout<<endl; list.clear(); } return 0; } ```
#include<iostream> using namespace std; int main(){ int i,j; int n,m; cin>>n; while(n!=0){ char t[n][2]; for(i=0;i<n;i++) cin>>t[i][0]>>t[i][1]; cin>>m; char ans[m+1]; for(i=0;i<m;i++){ cin>>ans[i]; for(j=0;j<n;j++){ if(t[j][0]==ans[i]){ ans[i]=t[j][1]; break; } } } ans[i]='\0'; cout<<ans<<endl; cin>>n; } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include<iostream> using namespace std; int main(){ int i,j; int n,m; cin>>n; while(n!=0){ char t[n][2]; for(i=0;i<n;i++) cin>>t[i][0]>>t[i][1]; cin>>m; char ans[m+1]; for(i=0;i<m;i++){ cin>>ans[i]; for(j=0;j<n;j++){ if(t[j][0]==ans[i]){ ans[i]=t[j][1]; break; } } } ans[i]='\0'; cout<<ans<<endl; cin>>n; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int N; char m; void solve(){ int M; map<char, char> changer; char f, t; for (int i = 0; i < N; i++){ cin >> f; cin >> t; changer[f] = t; } cin >> M; for (int i = 0; i < M; i++){ cin >> m; if (changer.find(m) != changer.end()) cout << changer[m]; else cout << m; } printf("\n"); } int main(){ while (cin >> N, N) solve(); return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include <bits/stdc++.h> using namespace std; int N; char m; void solve(){ int M; map<char, char> changer; char f, t; for (int i = 0; i < N; i++){ cin >> f; cin >> t; changer[f] = t; } cin >> M; for (int i = 0; i < M; i++){ cin >> m; if (changer.find(m) != changer.end()) cout << changer[m]; else cout << m; } printf("\n"); } int main(){ while (cin >> N, N) solve(); return 0; } ```
#include <iostream> using namespace std; int main() { int n,i; char c[75]={}; char d[75]={}; char a,b; for(int i=0; i<75 ;i++){ d[ i ]='0'+i; } while( true ){ cin >> n; if( n == 0 ){ return 0; } for(int i=0; i<75 ;i++){ c[ i ]=d[i]; } for( int i=0;i<n;i++){ cin >> a >> b; c[a - '0' ]=b; } cin >> n; for( int i=0;i<n;i++){ cin >> a; cout << c[a - '0']; } cout << endl; } }
### Prompt Please provide a CPP coded solution to the problem described below: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include <iostream> using namespace std; int main() { int n,i; char c[75]={}; char d[75]={}; char a,b; for(int i=0; i<75 ;i++){ d[ i ]='0'+i; } while( true ){ cin >> n; if( n == 0 ){ return 0; } for(int i=0; i<75 ;i++){ c[ i ]=d[i]; } for( int i=0;i<n;i++){ cin >> a >> b; c[a - '0' ]=b; } cin >> n; for( int i=0;i<n;i++){ cin >> a; cout << c[a - '0']; } cout << endl; } } ```
#include <cstdio> int main(){ char d[3],key[3],val[3],table[256]; int n,m,i; scanf("%d",&n); while(n>0){ for(i=0;i<256;i++) table[i]=i; for(i=0;i<n;i++){ scanf("%s %s",key,val); table[key[0]]=val[0]; }; scanf("%d",&m); for(i=0;i<m;i++){ scanf("%s",d); printf("%c",table[d[0]]); } printf("\n"); scanf("%d",&n); }; return 0; };
### Prompt Please formulate a cpp solution to the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include <cstdio> int main(){ char d[3],key[3],val[3],table[256]; int n,m,i; scanf("%d",&n); while(n>0){ for(i=0;i<256;i++) table[i]=i; for(i=0;i<n;i++){ scanf("%s %s",key,val); table[key[0]]=val[0]; }; scanf("%d",&m); for(i=0;i<m;i++){ scanf("%s",d); printf("%c",table[d[0]]); } printf("\n"); scanf("%d",&n); }; return 0; }; ```
#include <iostream> #include <string> using namespace std; int main() { int n,m,i,j,t[128]={}; char a,b; string s; while(cin>>n,n){ s=""; for(i=0;i<128;++i)t[i]=i; for(i=0;i<n;++i){ cin>>a>>b; t[a]=b; } cin>>m; for(i=0;i<m;++i){ cin>>a; s+=(char)t[a]; } cout<<s<<endl; } }
### Prompt Create a solution in Cpp for the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include <iostream> #include <string> using namespace std; int main() { int n,m,i,j,t[128]={}; char a,b; string s; while(cin>>n,n){ s=""; for(i=0;i<128;++i)t[i]=i; for(i=0;i<n;++i){ cin>>a>>b; t[a]=b; } cin>>m; for(i=0;i<m;++i){ cin>>a; s+=(char)t[a]; } cout<<s<<endl; } } ```
#include <cstdio> #include <algorithm> using namespace std; int n, m; char cv[256], ch, ch2; int main(){ while(scanf("%d\n", &n), n != 0) { fill(cv, cv + 256, 0); for (int i = 0; i < n; i++) { scanf("%c %c\n", &ch, &ch2); cv[ch] = ch2; } scanf("%d\n", &m); for (int i = 0; i < m; i++) { scanf("%c\n", &ch); printf("%c", cv[ch] == 0 ? ch : cv[ch]); } printf("\n"); } return 0; }
### Prompt Please formulate a CPP solution to the following problem: Create a program that converts data based on the given conversion table. The characters used in the data are letters or numbers, and the letters are case sensitive. There is no regularity in the order of the characters that appear in the conversion table. The conversion table has two characters (not a string), one before and one after, with a space in between. The conversion method is to convert the character before the line in the conversion table to the character after each time it appears in the data and output it. It is converted only once, and even if the converted character becomes the character to be converted again, it is not converted. Characters that do not appear in the conversion table are not converted and are output as they are. In the input file, the conversion table (first n + 1 line) is followed by the data to be converted (n + 2nd line and after). The number of lines in the conversion table is n on the first line, each line of the following n lines is two characters with a blank, and then the number of lines of data to be converted on the n + second line m, and each line of the following m lines. Is a single character. Let m ≤ 105. Output should be one line without blanks or line breaks as in the output example. Input example --- 3 A a 0 5 5 4 Ten A B C 0 1 Four Five a b b A Output example aBC5144aba input The input consists of multiple datasets. Input ends when n is 0. The number of datasets does not exceed 5. output For each data set, the converted character string is output on one line. Example Input 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 3 A a 0 5 5 4 10 A B C 0 1 4 5 a b A 0 Output aBC5144aba aBC5144aba ### Response ```cpp #include <cstdio> #include <algorithm> using namespace std; int n, m; char cv[256], ch, ch2; int main(){ while(scanf("%d\n", &n), n != 0) { fill(cv, cv + 256, 0); for (int i = 0; i < n; i++) { scanf("%c %c\n", &ch, &ch2); cv[ch] = ch2; } scanf("%d\n", &m); for (int i = 0; i < m; i++) { scanf("%c\n", &ch); printf("%c", cv[ch] == 0 ? ch : cv[ch]); } printf("\n"); } return 0; } ```