2016年第七届蓝桥杯C/C++ B组国赛 —— 第三题:棋子换位
生活随笔
收集整理的這篇文章主要介紹了
2016年第七届蓝桥杯C/C++ B组国赛 —— 第三题:棋子换位
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
棋子換位
有n個(gè)棋子A,n個(gè)棋子B,在棋盤(pán)上排成一行。
它們中間隔著一個(gè)空位,用“.”表示,比如:
AAA.BBB
現(xiàn)在需要所有的A棋子和B棋子交換位置。
移動(dòng)棋子的規(guī)則是:
AAA.BBB 可以走法:
移動(dòng)A ==> AA.ABBB 移動(dòng)B ==> AAAB.BB跳走的例子:
AA.ABBB ==> AABA.BB以下的程序完成了AB換位的功能,請(qǐng)仔細(xì)閱讀分析源碼,填寫(xiě)劃線(xiàn)部分缺失的內(nèi)容。
#include <stdio.h> #include <string.h>void move(char* data, int from, int to) {data[to] = data[from];data[from] = '.'; }int valid(char* data, int k) {if(k<0 || k>=strlen(data)) return 0;return 1; }void f(char* data) {int i;int tag;int dd = 0; // 移動(dòng)方向while(1){tag = 0;for(i=0; i<strlen(data); i++){if(data[i]=='.') continue;if(data[i]=='A') dd = 1;if(data[i]=='B') dd = -1;if(valid(data, i+dd) && valid(data,i+dd+dd) && data[i+dd]!=data[i] && data[i+dd+dd]=='.'){ //如果能跳... move(data, i, i+dd+dd);printf("%s\n", data);tag = 1;break;}}if(tag) continue;for(i=0; i<strlen(data); i++){if(data[i]=='.') continue;if(data[i]=='A') dd = 1;if(data[i]=='B') dd = -1; if(valid(data, i+dd) && data[i+dd]=='.'){ // 如果能移動(dòng)...if( ______________________ ) continue; //填空位置 move(data, i, i+dd);printf("%s\n", data);tag = 1;break;}}if(tag==0) break; } }int main() {char data[] = "AAA.BBB"; f(data);return 0; }注意:只提交劃線(xiàn)部分缺少的代碼,不要復(fù)制已有代碼或填寫(xiě)任何多余內(nèi)容。
Code
/*^....0^ .1 ^1^.. 011.^ 1.0^ 1 ^ ^0.11 ^ ^..^0. ^ 0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. 01. . 1. .^1. 1 1. ^0^ . ^.1 00 01^.0 001. .^*/ // VB_king —— 2016_Finals_B_C++_3.cpp created by VB_KoKing on 2019-05-19:23. /* Procedural objectives:Variables required by the program:Procedural thinking:程序的第一個(gè)循環(huán)處理的是可以跳的情況,第二個(gè)循環(huán)處理的是可以走的情況,而在while循環(huán)中都是先把能跳的處理完之后在處理能走的;需要填空的地方代表可以走但是不走的情況,對(duì)比源程序走法和正確走法:源程序走法為: AA.ABBB AABA.BB AAB.ABB A.BAABB .ABAABB BA.AABB B.AAABB 正確的走法為: AA.ABBB AABA.BB AABAB.B AAB.BAB A.BABAB .ABABAB BA.ABAB BABA.AB BABABA. BABAB.A BAB.BAA B.BABAA BB.ABAA BBBA.AA BBB.AAA這個(gè)規(guī)律比較詭異,當(dāng)左右兩邊的字母不相同("."不算在內(nèi))時(shí),可以移動(dòng)的那個(gè)字母;當(dāng)左右兩邊的字母相同時(shí),可以移動(dòng)但不要移動(dòng)。Functions required by the program:Determination algorithm:Determining data structure:*/ /* My dear Max said: "I like you, So the first bunch of sunshine I saw in the morning is you, The first gentle breeze that passed through my ear is you, The first star I see is also you. The world I see is all your shadow."FIGHTING FOR OUR FUTURE!!! */ #include <stdio.h> #include <string.h>void move(char* data, int from, int to) {data[to] = data[from];data[from] = '.'; }int valid(char* data, int k) {if(k<0 || k>=strlen(data)) return 0;return 1; }void f(char* data) {int i;int tag;int dd = 0; // 移動(dòng)方向while(1){tag = 0;for(i=0; i<strlen(data); i++){if(data[i]=='.') continue;if(data[i]=='A') dd = 1;if(data[i]=='B') dd = -1;if(valid(data, i+dd) && valid(data,i+dd+dd)&& data[i+dd]!=data[i] && data[i+dd+dd]=='.'){//如果能跳...move(data, i, i+dd+dd);printf("%s\n", data);tag = 1;break;}}if(tag) continue;for(i=0; i<strlen(data); i++){if(data[i]=='.') continue;if(data[i]=='A') dd = 1;if(data[i]=='B') dd = -1;if(valid(data, i+dd) && data[i+dd]=='.'){// 如果能移動(dòng)...if(valid(data, i+dd+dd) && (valid(data,i-dd)) && data[i+dd+dd] == data[i-dd]) continue;move(data, i, i+dd);printf("%s\n", data);tag = 1;break;}}if(tag==0) break;} }int main() {char data[] = "AAA.BBB";f(data);return 0; }總結(jié)
以上是生活随笔為你收集整理的2016年第七届蓝桥杯C/C++ B组国赛 —— 第三题:棋子换位的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 2018年第九届蓝桥杯C/C++ C组国
- 下一篇: 2016年第七届蓝桥杯 - 国赛 - C