NQUEEN - Yet Another N-Queen Problem

no tags 

After solving Solution to the n Queens Puzzle by constructing, LoadingTime wants to solve a harder version of the N-Queen Problem. Some queens have been set on particular locations on the board in this problem. Can you help him??

Input

The input contains multiple test cases. Every line begins with an integer N (N<=50), then N integers followed, representing the column number of the queen in each rows. If the number is 0, it means no queen has been set on this row. You can assume there is at least one solution.

Output

For each test case, print a line consists of N numbers separated by spaces, representing the column number of the queen in each row. If there are more than one answer, print any one of them.

Example

Input:
4 0 0 0 0
8 2 0 0 0 4 0 0 0

Output:
2 4 1 3
2 6 1 7 4 8 3 5

hide comments
nightwolf_9197: 2017-04-02 15:54:17

how to avoid TLE ? what is expected time complexity.

adichd123: 2016-07-30 08:29:22

how to avoid tle???

Rishit Sanmukhani: 2015-07-08 12:46:57

Really good problem. Can be solved by converting to exact cover problem.

Gavin Liu: 2015-01-28 03:09:02

the time limit maybe too strict for python

zhangyw: 2012-01-29 09:29:27

Just cover n columns...

Last edit: 2012-01-29 09:31:26
Prabhat Chaudhary: 2011-07-17 03:57:08

when do we consider that the input has terminated??

cherudim: 2011-07-17 03:57:08

It drives me cracy

Last edit: 2011-01-25 08:03:49
DAGA PIYUSH: 2011-07-17 03:57:08

What is the exit condition?

Re by Blue Mary: Input terminates by EOF.

Last edit: 2010-01-26 07:20:17

Added by:Bin Jin
Date:2007-09-06
Time limit:0.640s
Source limit:10000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: CPP
Resource:own problem