CSU's mutilated chessboard (BFS)

Source: Internet
Author: User

Description

Input

The input contains no more than 10000 sets of data. Each set of data contains 6 integers r1, C1, R2, C2, R3, C3 (1<=r1, C1, R2, C2, R3, c3<=8). Three lattice A, B, C are guaranteed to be different.

Output

For each set of data, the test point number and the minimum number of steps are output.

Sample Input
<span class= "Sampledata" >1 1 8 7 5 (1 3 3 2 2</span>
Sample Output
<span class= "Sampledata" >case 1:7case 2:3</span> 
<span class= "Sampledata" ></span><pre name= "code" class= "HTML" > #include <iostream> #include <cstdio> #include <cstring> #include <queue>using namespace Std;int di,dj,r,c;int m[9][9];struct node {int i,j,time;}; Queue<node>qq;int x[8]={0,1,1,1,0,-1,-1,-1},y[8]={1,1,0,-1,-1,-1,0,1};void BFs () {while (!qq.empty ()) {node Temp=qq.front (); node T=temp;qq.pop (); if (t.i==r&&t.j==c) break;for (int i=0;i<8;i++) {t=temp;t.i+=x[i]; T.j+=y[i];t.time+=1;if (t.i<1 | | t.i>8 | | t.j<1 | | t.j>8) continue;if (t.i==r && t.j==c) continue;if (T.TIME&LT;M[T.I][T.J]) {M[t.i][t.j]=t.time;qq.push (t);}}} int main () {int Si,sj,i,j,sigh;sigh=0;while (cin>>si>>sj>>di>>dj>>r>>c) {for (i= 1;i<9;i++) {for (j=1;j<9;j++) m[i][j]=10000;} M[si][sj]=0;while (!qq.empty ()) Qq.pop (), node Temp={si,sj,0};qq.push (temp), BFS ();p rintf ("Case%d:%d\n", ++sigh,m[di ][DJ]);}}


<span class= "Sampledata" ></span>

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

CSU's mutilated chessboard (BFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.