标签:
8 8 3 3 3 4 5 6 6 2 1 7 7
11 96
NOI’2005福建省选手选拔赛
#include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> #include<stdlib.h> using namespace std; struct node { int x; int y; }a,b; int jx[] = {0,1,0,-1}; int jy[] = {1,0,-1,0}; int v[101][101]; int map[101][101]; int n,m,k; int flag; int ans; int psum; void DFS(int xx,int yy,int cnt) { if(cnt>ans) { return ; } if(xx == b.x && yy == b.y) { if(cnt<ans) { flag = 1; ans = cnt; psum = 1; } else if(cnt == ans) { psum++; } return ; } for(int i=0;i<4;i++) { if(xx+jx[i]>=0 && xx+jx[i]<n && yy+jy[i]>=0 && yy+jy[i]<m && map[xx+jx[i]][yy+jy[i]] == 0 && v[xx+jx[i]][yy+jy[i]] == 0) { v[xx+jx[i]][yy+jy[i]] = 1; DFS(xx+jx[i],yy+jy[i],cnt+1); v[xx+jx[i]][yy+jy[i]] = 0; } } } int main() { while(scanf("%d%d%d",&n,&m,&k)!=EOF) { for(int i=0;i<=n;i++) { for(int j=0;j<=m;j++) { map[i][j] = 0; v[i][j] = 0; } } int x,y; for(int i=0;i<k;i++) { scanf("%d%d",&x,&y); map[x][y] = 1; } ans = 99999999; flag = 0; psum = 0; scanf("%d%d",&a.x,&a.y); scanf("%d%d",&b.x,&b.y); DFS(a.x,a.y,0); if(flag == 0) { printf("No Solution!\n"); } else { printf("%d\n",ans); printf("%d\n",psum); } } return 0; }
标签:
原文地址:http://blog.csdn.net/yeguxin/article/details/45499003