-
个人简介
https://kaoshi.wjx.top/vm/YDnBsBK.aspx#
//华容道 //author:Mitchell_Donovan //Date:2021.3.13 #include<iostream> #include<stdlib.h> using namespace std; class HUARONG { private: int index; int* array; public: HUARONG(int n) { index = n; array = new int [index * index]; for (int i = 0; i < index * index; i++) { array[i] = i; } for (int i = 0; i < index * index; i++) { int x = array[i]; int pos = rand() % (n * n); array[i] = array[pos]; array[pos] = x; } } void print() { for (int i = 0; i < index * index; i++) { if (array[i] == 0) { cout.width(4); cout<<" "; } else { cout.width(4); cout << array[i]; } if (i % index == index - 1) { cout << endl; } } } bool isCorrect(){ for (int i = 0; i < index * index - 1; i++) { if (array[i] != i + 1) { return false; } } return true; } void play() { print(); while (isCorrect() != true) { move(); print(); } cout << "成功!" << endl; } void move() { char a; do { cout << "请输入移动方向↑(w)↓(s)←(a)→(d)——"; cin >> a; } while (!((a == 's' && (whereBlank() - index) >= 0) || (a == 'w' && (whereBlank() + index) < index * index) || (a == 'd' && (whereBlank() - 1) >= 0 && whereBlank() % index != 0) || (a == 'a' && (whereBlank() + 1) < index * index && (whereBlank() + 1) % index != 0))); if (a == 'w') { int pos = whereBlank(); int x = array[pos]; array[pos] = array[pos + index]; array[pos + index] = x; } else if (a == 's') { int pos = whereBlank(); int x = array[pos]; array[pos] = array[pos - index]; array[pos - index] = x; } else if (a == 'a') { int pos = whereBlank(); int x = array[pos]; array[pos] = array[pos + 1]; array[pos + 1] = x; } else if (a == 'd') { int pos = whereBlank(); int x = array[pos]; array[pos] = array[pos - 1]; array[pos - 1] = x; } } int whereBlank() { for (int i = 0; i < index * index; i++) { if (array[i] == 0 ) { return i; } } } }; int main() { int n; cout << "请输入华容道的阶数:"; cin >> n; HUARONG a(n); a.play(); }
#include <iostream> #include <vector> // 检查是否有玩家获胜 bool checkWin(const std::vector<std::vector<char>>& board, char player) { // 检查行 for (int i = 0; i < 3; ++i) { if (board[i][0] == player && board[i][1] == player && board[i][2] == player) { return true; } } // 检查列 for (int j = 0; j < 3; ++j) { if (board[0][j] == player && board[1][j] == player && board[2][j] == player) { return true; } } // 检查对角线 if (board[0][0] == player && board[1][1] == player && board[2][2] == player) { return true; } if (board[0][2] == player && board[1][1] == player && board[2][0] == player) { return true; } return false; } // 检查棋盘是否已满 bool isBoardFull(const std::vector<std::vector<char>>& board) { for (const auto& row : board) { for (char cell : row) { if (cell == ' ') { return false; } } } return true; } // 打印棋盘 void printBoard(const std::vector<std::vector<char>>& board) { std::cout << " 0 1 2" << std::endl; for (int i = 0; i < 3; ++i) { std::cout << i << " "; for (int j = 0; j < 3; ++j) { std::cout << board[i][j]; if (j < 2) { std::cout << "|"; } } std::cout << std::endl; if (i < 2) { std::cout << " -+-+-" << std::endl; } } } int main() { std::vector<std::vector<char>> board(3, std::vector<char>(3, ' ')); char currentPlayer = 'X'; while (true) { printBoard(board); int row, col; std::cout << "玩家 " << currentPlayer << " 的回合,请输入行和列 (例如: 0 1): "; std::cin >> row >> col; // 检查输入是否合法 if (row < 0 || row >= 3 || col < 0 || col >= 3 || board[row][col] != ' ') { std::cout << "无效的输入,请重新输入。" << std::endl; continue; } // 更新棋盘 board[row][col] = currentPlayer; // 检查是否有玩家获胜 if (checkWin(board, currentPlayer)) { printBoard(board); std::cout << "玩家 " << currentPlayer << " 获胜!" << std::endl; break; } // 检查是否平局 if (isBoardFull(board)) { printBoard(board); std::cout << "平局!" << std::endl; break; } // 切换玩家 currentPlayer = (currentPlayer == 'X') ? 'O' : 'X'; } return 0; }
//24点2.0 #include<bits/stdc++.h> using namespace std; int a[10],b[10],x[10],y[10]; void sc() { for(int i=1;i<=3;i++) { if(x[i]<y[i])swap(x[i],y[i]); cout<<x[i]; if(b[i]==1)cout<<"+"<<y[i]<<"="<<x[i]+y[i]; if(b[i]==2)cout<<"-"<<y[i]<<"="<<x[i]-y[i]; if(b[i]==3)cout<<"*"<<y[i]<<"="<<x[i]*y[i]; if(b[i]==4)cout<<"/"<<y[i]<<"="<<x[i]/y[i]; cout<<"\n"; } } bool ck() { for(int i=1;i<=4;i++)if(a[i]==24)return 1; return 0; } void dfs(int t) { if(t>3) { if(ck()) { sc(); exit(0); } return; } for(int i=2;i<=4;i++) { for(int j=1;j<=4;j++) { if(i!=j&&a[j]!=-1&&a[i]!=-1) { for(int k=1;k<=4;k++) { x[t]=a[i]; y[t]=a[j]; int l=a[i],r=a[j]; if(k==1) { b[t]=1; a[i]=l+r; } if(k==2) { b[t]=2; a[i]=abs(l-r); } if(k==3) { b[t]=3; a[i]=l*r; } if(k==4) { if(a[j]==0||l%r!=0)continue; b[t]=4; a[i]=l/r; } a[j]=-1; dfs(t+1); a[i]=l; a[j]=r; } } } } } int main() { //freopen("Poker.in","r",stdin); //freopen("Poker.out","w",stdout); cin>>a[1]>>a[2]>>a[3]>>a[4]; dfs(1); cout<<"No answer!"; return 0; }
-
通过的题目
-
最近活动
This person is lazy and didn't join any contests or homework. -
最近编写的题解
This person is lazy and didn't write any solutions.
题目标签
- 聪明人游戏
- 1