歡樂星星碰用C++程序解決星星碰問題
歡樂星星碰用C++程序解決星星碰問題如下:
#include
using namespace std;
int map[100][100];
int ans[100][100];
int n,m;
bool check_map(){
? ? for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)if(map[i][j]==1)return false;
? ? return true;
}
void change_light(int x,int y){
? ? if(map[x][y]>=0)map[x][y]^=1;
? ? if(map[x+1][y]>=0)map[x+1][y]^=1;
? ? if(map[x][y+1]>=0)map[x][y+1]^=1;
? ? if(map[x-1][y]>=0)map[x-1][y]^=1;
? ? if(map[x][y-1]>=0)map[x][y-1]^=1;
? ?
? ?
}
bool dfs(int x,int y){
? ? if(x>n){
? ? ? ? return check_map();
? ? }
? ? if(y>m){
? ? ? ? return dfs(x+1,1);
? ? }
? ? if(map[x][y]==-1)return dfs(x,y+1);
? ? // test 0:
? ? ans[x][y]=0;
? ? bool ans2 = dfs(x,y+1);
? ? if(ans2)return true;
? ? // test 1:
? ? ans[x][y]=1;
? ? change_light(x,y);
? ? ans2 = dfs(x,y+1);
? ? if(ans2)return true;
? ? change_light(x,y);
? ? ans[x][y]=0;
? ? return false;
}
int main(){
? ? cin>>n>>m;
? ? char c;
? ? for(int i=0;i<=n+1;i++)for(int j=0;j<=m+1;j++)map[i][j]=-1;
? ? for(int i=1;i<=n;i++){
? ? ? ? string str;
? ? ? ? cin>>str;
? ? ? ? for(int j=1;j<=m;j++){
? ? ? ? ? ? c= str[j-1];
? ? ? ? ? ? if(c=='#')map[i][j]=-1;
? ? ? ? ? ? if(c=='*')map[i][j]=1;
? ? ? ? ? ? if(c=='.')map[i][j]=0;
? ? ? ? }
? ? }
? ? dfs(1,1);
? ? for(int i=1;i<=n;i++){
? ? ? ? for(int j=1;j<=m;j++){
? ? ? ? ? ? if(map[i][j]<0)cout<<"#";
? ? ? ? ? ? else cout< ? ? ? ? } ? ? ? ? cout< ? ? } } 先輸入行數(shù)和列數(shù) 然后輸入地圖,點表示空格,*表示星星,# 表示墻壁。注意地圖外的格子需要全部標(biāo)成墻壁。 然后程序會輸出一個相同大小的包含0,1和# 的answer,表示解法。0表示該格子需要按偶數(shù)遍,如果沒有星星可以把相鄰的按一下然后這個點按兩次。1表示該格子需要按奇數(shù)遍。 實測任何游戲中的題都可以用該程序間接解決 以上就是歡樂星星碰用C++程序解決星星碰問題相關(guān)內(nèi)容。
閩公網(wǎng)安備 35021102000359號
網(wǎng)絡(luò)文化經(jīng)營許可證號:閩網(wǎng)文(2016)4364-073號