| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
在vs2017上运行就没有问题,运行结果输入输出没什么毛病啊,为什么在这里提交老是Wrong Answer呢?#include <iostream>
#include <stdio.h>
using namespace std;
int chess[4][4];
int moves = 17;//4*4+1
//输入:创建棋盘
void buildBoard()
{
char c;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
cin >> c;
if (c == 'w')
chess[i][j] = 0;
else
chess[i][j] = 1;
}
}
}
//单个棋子翻转
void turn(int x,int y)
{
if (x >= 0 && y >= 0 && x, 4 && y, 4)
{
chess[x][y] = !chess[x][y];
}
}
//棋盘翻转操作函数
void flip(int num)
{
int i = num / 4;
int j = num % 4;
turn(i, j);
turn(i + 1, j);
turn(i - 1, j);
turn(i, j + 1);
turn(i, j - 1);
}
//判断棋盘是否全部一样
bool AllSame()
{
int sum = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
sum += chess[i][j];
}
}
return !(sum % 16);
}
//深度优先搜索函数
void dfs(int pos, int num_flipped)
{
if (AllSame())
{
if (num_flipped < moves)
moves = num_flipped;
return;
}
if (pos >= 16)
return;
dfs(pos + 1, num_flipped);
flip(pos);
dfs(pos + 1, num_flipped + 1);
flip(pos);
}
int main()
{
buildBoard();
dfs(0, 0);
if (moves == 17)
cout << "Impossible" << endl;
else
cout << moves << endl;
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator