CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » December 2009 (Contest XI) » The N Queens Puzzle Revisited

The N Queens Puzzle Revisited

Problem code: J3

  • All Submissions

All submissions for this problem are available.

Johnny, a computer science student from the Bytelandian Technological University (BTU), has finally managed to write a program to solve the N queens problem. However, his version of the N queens problem is not really all that similar to the classical one.

Recall that in the N queens problem, there is a square chessboard of size NxN. The goal is to put N queens on the chessboard so that no two queens would be able to attack each other.

In Johnny's version of the problem (which he states to be "much harder" than the original one), there are K "blocked" squares given on the chessboard. The goal is still the same: to put N queens on the chessboard such that no two queens would be able to attack each other. However, there are some differences due to the existence of blocked squares. First, a queen cannot be put on a blocked square. Secondly, two queens are considered to be able to attack each other only if there is no blocked square between them. Finally, there must be exactly one queen on every column of the board.

Johnny claims that his program solves the above problem very effectively. He generates some test cases (which his program can solve within minutes) to challenge you. You know the method Johnny used to generate the test case. For a given N, Johnny randomly picks a number K between 1 and N2/2 with equal probability. After that, Johnny picks K blocked squares on the chess board. Any combinations of K squares can be picked with equal probability.

Write a program that can solve Johnny's test cases within seconds.

Input

The first line contains t, the number of test cases (about 5). Then t test cases follow. Each test case has the following form:

  • The first line contains two integers N and K (8 <= N <= 500, 1 <= K <= N2/2)
  • Each line in the next K lines contains two integers i, j which are the row and the column number of a blocked square (1 <= i, j <= N)

Each test case's input is separated by a blank line. The rows and the columns of the chessboard are numbered from 1 to N. There is always a solution for each test case.

Output

For each test case, print a single line containing N integers a1, a2, ..., aN in which ai is the row number of the queen in column i. Any correct solution will be accepted.

Example

Input
1

8 15
7 7
3 1
7 1
4 8
8 3
6 5
4 4
2 1
8 2
6 3
3 7
7 6
2 3
1 2
6 4

Output
4 2 7 5 2 4 6 8


Author: admin
Date Added: 15-10-2009
Time Limit: 1 - 3 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

is there a blank line after

surendra @ 2 Dec 2009 01:20 PM

is there a blank line after the no. of testcases t i.e 1 here

blank line after 1 or not

Yes.

admin @ 2 Dec 2009 04:40 PM

Yes.

How can i take randomly n and

sitaramkola @ 2 Dec 2009 06:28 PM

How can i take randomly n and k values

Kola: you don't have to. Let

jcomeau_ictx @ 3 Dec 2009 10:08 AM

Kola: you don't have to. Let Johnny worry about that part :^)

can anyone tell me the format

Wassim @ 4 Dec 2009 06:07 PM

can anyone tell me the format of the input and out of many test cases because each time I submit a working solution it gives me wrong submition. Does the follwing format correct?

 

<Test Cases Number>

<Empty Line>

<Case1>

<Empty Line>

<Case2>

<Empty Line>

<Case3>

<Empty Line>

<Answer Case1>

<Answer Case2>

<Answer Case3>

Wassim: I can see you are

wasi @ 4 Dec 2009 06:39 PM

Wassim: I can see you are using C++. IMO you don't have to worry about stuff line blank lines if you use scanf/cin.

so why it gives me wrong

Wassim @ 4 Dec 2009 06:47 PM

so why it gives me wrong answer? I have tried my code on the above sample and it gave me different solution but also valid.Aslo, I ve tried it on other random solutions and it worked to. Therefore, I thought tht the problem may be beacause of the format of the output

Yeah, It wouldn't really kill

Cheesecake @ 4 Dec 2009 10:50 PM

Yeah, It wouldn't really kill them to give TWO test cases.

Are there any special cases,

snktagarwal @ 9 Dec 2009 09:39 AM

Are there any special cases, or boundary conditions involved ? I know it is not wise to ask for test cases, but I am stuck at wrong answer :(.

I'm afraid you won't get

triplem @ 9 Dec 2009 10:11 AM

I'm afraid you won't get hints until after the contest.

My error message says that

snktagarwal @ 9 Dec 2009 10:25 AM

My error message says that the program ran for 0.00 seconds using 1.8 MiB but produced the wrong result! Why is the time 0.00 seconds ? What does this imply ?

It implies you got a wrong

triplem @ 9 Dec 2009 10:39 AM

It implies you got a wrong answer very quickly :P

^^Haha! But seriously, all

Cheesecake @ 10 Dec 2009 11:25 AM

^^Haha!

But seriously, all this question has done is make me feel very, very stupid. :|

This question on Topcoder

rajatag12 @ 10 Dec 2009 04:17 PM

This question on Topcoder would have caused either reaalllyyyy low submissions or reallllyyyyyy high challenge cases.. :)

@Rajat - this kind of problem

innocentboy @ 10 Dec 2009 09:27 PM

@Rajat - this kind of problem cant be given in topcoder. Specially when they guarantee that all the testcases are randomly generated, so the question of challenge doesnt arise.

its really tough to analyse

surendra @ 11 Dec 2009 02:46 PM

its really tough to analyse these kind of problems, i hope somebody would put a nice tutorial in wiki for solving constraint satisfaction problem like sudokux, nqueens in polynomial time and different algorithms to solve these like dancing links, forward search or min conflict heuristic after the contest gets over.

I saw the solutions in the

snktagarwal @ 18 Dec 2009 10:04 AM

I saw the solutions in the successful submissions, and it seemed pretty straight forward.. I mean take a col, place a queen, update the board, and move to next col. Roll back if necessarsy and so on!

Can anyone tell if there are some cornor cases which I left, or *challenges* which were specific to this problem ?

I don't think there is any

imrankane2005 @ 18 Dec 2009 12:43 PM

I don't think there is any corner cases, it's a pretty straightforward as you mentioned.

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold
CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com