Algorithm to train black and white impermanence
Time limit: 1.0s Memory Limit: 256.0MB
Description of the problem
in a dorm room are ready after completion of the academic playing a game: The game is like this, everyone heads were put up a white or black paper, and now everyone will say the word "I saw Zhang x y white paper and sheets of black paper ", but also known to each person's head against the white paper is telling the truth, every head against black paper people say is a lie, and now ask you what the head judge close on the white paper, the output if no solution "NoSolution."; if a plurality of solutions, the number of people put the white bars are sized according to the composition of a number (such as the first answer in each individual stickers and a third person close to the head of the white paper, then the number is 13; if the first 6,7,8 individuals have posted a white paper, then the number is 678) that outputs the smallest number (if all all black paper also meet the case, then the output 0)
Input Format
The first line an integer n, followed by n lines in the i-th row two integers x and y, respectively, the i-th person says, "I saw the x and y sheets of white paper sheets of black paper."
Output format
line. If no solution output "NoSolution.". Otherwise the output values in the answer (see specific description of the problem) the smallest one, if all are black strips also meet the case, then the output 0
Sample input
2
. 1 0
. 1 0
Sample output
0
Sample input
. 5
. 3. 1
0. 4
. 1. 3
. 4 0
. 1. 3
Sample output
35
Data size and conventions
n <= 8
#include <stdio.h>
#define BLACK 0
#define WHITE 1
int n;
int x[10], y[10];
int slip[10];
int solve(int person_id)
{
if (person_id > 0)
{
slip[person_id] = BLACK;
if (solve(person_id - 1))
return 1;
slip[person_id] = WHITE;
return solve(person_id - 1);
}
else
{
int n_black = 0, n_white = 0;
for (int i = 1; i <= n; ++i)
{
if (slip[i] == BLACK)
n_black++;
else
n_white++;
}
for (int i = 1; i <= n; ++i)
{
if (slip[i] == WHITE)
{
if (!(x[i] == n_white - 1 && y[i] == n_black))
return 0;
}
else
{
if (x[i] == n_white && y[i] == n_black - 1)
return 0;
}
}
return 1;
}
}
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d %d", &x[i], &y[i]);
if (solve(n))
{
int n_black = 0, n_white = 0;
for (int i = 1; i <= n; ++i)
{
if (slip[i] == BLACK)
n_black++;
else
n_white++;
}
if (n_black == n)
printf("0");
else
{
for (int i = 1; i <= n; ++i)
{
if (slip[i] == WHITE)
printf("%d", i);
}
}
}
else
printf("NoSolution.");
return 0;
}