【nwpu暑假集训搜索专题】(一)

表示我都没有跟着群巨暑假集训做题的节奏走,但是由于搜索太弱,所以就做了几道搜索题。

POJ 3278

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
using namespace std;
int n, k, maxn = 100001;
int a[100100];
int q[100100];
int bfs()
{
	memset(a, 0, sizeof(a));
	a[n] = 1;
	int l = 0, r = 1;
	q[0] = n; 
	while (l < r)
	{
		int x = q[l++];
	
		if (!a[x - 1] && (x - 1 >= 0))
		{
			a[x - 1] = a[x] + 1;
			q[r++] = x - 1;
			if (x - 1 == k) return a[x] + 1; 
		
		}
		if (!a[x + 1] && (x + 1 < maxn))
		{
			a[x + 1] = a[x] + 1;
			q[r++] = x + 1;
			if (x + 1 == k) return a[x] + 1;
		
		}
		if (!a[x * 2] && (x * 2 < maxn))
		{
			a[x * 2] = a[x] + 1;
			q[r++] = x * 2;
			if (x * 2 == k) return a[x] + 1;
			
		}
	}
	
}
int main()
{
	while (scanf("%d%d", &n, &k) != EOF)
	{
		if (n == k) printf("0\n");
		else printf("%d\n", bfs() - 1);
		
	}
	return 0;
}

POJ 1979

数联通块数目

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n, m, sx, sy;
char a[200][200];
void init()
{
    memset(a, 0, sizeof(a));

    for(int i = 1;i <= n; i++)
    for(int j = 1; j <= m; j++)
    {
        cin >> a[i][j];
        if (a[i][j] == '@')
        {
            sx = i;
            sy = j;
        }
        if ((a[i][j] == '.') || (a[i][j] == '@'))
           a[i][j] = 1;
        else a[i][j] = 0;
    }
}
long dfs(int i,int j)
{
    long k = 1;
    a[i][j] = 0;
    if(a[i - 1][j] > 0)  k += dfs(i - 1, j);
    if(a[i + 1][j] > 0)  k += dfs(i + 1, j);
    if(a[i][j - 1] > 0)  k += dfs(i, j - 1);
    if(a[i][j + 1] > 0)  k += dfs(i, j + 1);
    return k;

}


int main()
{
 
    cin >> m >> n;
    while ((m != 0) && (n != 0))
    {
        init();
        cout << dfs(sx, sy) << endl;
        cin >> m >> n;
    }

    return 0;

}

POJ 2243

骑士游历问题(写得好丑好丑哦)

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
using namespace std;
int v[400][400];
int qx[200000], qy[200000];
int t, n = 8, sx, tx, sy, ty;
bool can (int x, int y)
{
	if (x < 0) return false;
	if (y < 0) return false;
	if (x >= n) return false;
	if (y >= n) return false;
	if (v[x][y] != 0) return false;
	return true;
}

int bfs()
{
	memset(v, 0, sizeof(v));
	v[sx][sy] = 1;
	int l = 0, r = 1;
	qx[0] = sx;
	qy[0] = sy;

	while (l < r)
	{
		int x = qx[l], y = qy[l++];
		if (x == tx && y == ty) return v[x][y];
		if (can(x - 1, y - 2))
		{
			v[x-1][y-2] = v[x][y] + 1;
			qx[r] = x-1; qy[r++] = y-2;
		}
		if (can(x - 2, y - 1))
		{
			v[x-2][y-1] = v[x][y] + 1;
			qx[r] = x-2; qy[r++] = y-1;
		}
		if (can(x - 2, y + 1))
		{
			v[x-2][y+1] = v[x][y] + 1;
			qx[r] = x-2; qy[r++] = y+1;
		}
		if (can(x - 1, y + 2))
		{
			v[x-1][y+2] = v[x][y] + 1;
			qx[r] = x-1; qy[r++] = y+2;
		}


		if (can(x + 1, y - 2))
		{
			v[x+1][y-2] = v[x][y] + 1;
			qx[r] = x+1; qy[r++] = y-2;
		}
		if (can(x + 2, y - 1))
		{
			v[x+2][y-1] = v[x][y] + 1;
			qx[r] = x+2; qy[r++] = y-1;
		}
		if (can(x + 2, y + 1))
		{
			v[x+2][y+1] = v[x][y] + 1;
			qx[r] = x+2; qy[r++] = y+1;
		}
		if (can(x + 1, y + 2))
		{
			v[x+1][y+2] = v[x][y] + 1;
			qx[r] = x+1; qy[r++] = y+2;
		}

	}

}

int main()
{
	char c1, c2;
	int y1, y2;
    while (scanf(" %c%d %c%d", &c1, &y1, &c2, &y2) != EOF)
    {
    	
		sx = c1 - 'a';
		sy = y1 - 1;
		tx = c2 - 'a';
		ty = y2 - 1;
		//cout << sx<< sy << tx << ty<< endl;
        printf("To get from %c%d to %c%d takes %d knight moves.\n",c1, y1, c2, y2, bfs() - 1);

    }



	return 0;
}

POJ 1915

继续骑士游历

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
using namespace std;
int v[400][400];
int qx[200000], qy[200000];
int t, n, sx, tx, sy, ty;
bool can (int x, int y)
{
	if (x < 0) return false;
	if (y < 0) return false;
	if (x >= n) return false;
	if (y >= n) return false;
	if (v[x][y] != 0) return false;
	return true;
}

int bfs()
{
	memset(v, 0, sizeof(v));
	v[sx][sy] = 1;
	int l = 0, r = 1;
	qx[0] = sx;
	qy[0] = sy;

	while (l < r)
	{
		int x = qx[l], y = qy[l++];
		if (x == tx && y == ty) return v[x][y];
		if (can(x - 1, y - 2))
		{
			v[x-1][y-2] = v[x][y] + 1;
			qx[r] = x-1; qy[r++] = y-2;
		}
		if (can(x - 2, y - 1))
		{
			v[x-2][y-1] = v[x][y] + 1;
			qx[r] = x-2; qy[r++] = y-1;
		}
		if (can(x - 2, y + 1))
		{
			v[x-2][y+1] = v[x][y] + 1;
			qx[r] = x-2; qy[r++] = y+1;
		}
		if (can(x - 1, y + 2))
		{
			v[x-1][y+2] = v[x][y] + 1;
			qx[r] = x-1; qy[r++] = y+2;
		}


		if (can(x + 1, y - 2))
		{
			v[x+1][y-2] = v[x][y] + 1;
			qx[r] = x+1; qy[r++] = y-2;
		}
		if (can(x + 2, y - 1))
		{
			v[x+2][y-1] = v[x][y] + 1;
			qx[r] = x+2; qy[r++] = y-1;
		}
		if (can(x + 2, y + 1))
		{
			v[x+2][y+1] = v[x][y] + 1;
			qx[r] = x+2; qy[r++] = y+1;
		}
		if (can(x + 1, y + 2))
		{
			v[x+1][y+2] = v[x][y] + 1;
			qx[r] = x+1; qy[r++] = y+2;
		}

	}

}

int main()
{
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d", &n);
        scanf("%d%d", &sx, &sy);
        scanf("%d%d", &tx, &ty);
        printf("%d\n", bfs() - 1);

    }



	return 0;
}

POJ 3984

走迷宫搜方案

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
using namespace std;
int a[5][5];
int v[5][5];
int qx[40], qy[40];
bool can (int x, int y)
{
	if (x < 0) return false; 
	if (y < 0) return false; 
	if (x >= 5) return false; 
	if (y >= 5) return false; 
	if (a[x][y] == 1) return false;
	if (v[x][y] != 0) return false;
	return true;
}

void bfs()
{
	memset(v, 0, sizeof(v));
	v[0][0] = 1;
	int l = 0, r = 1;
	qx[0] = 0; 
	qy[0] = 0; 

	while (l < r)
	{
		int x = qx[l], y = qy[l++];
		if (can(x - 1, y))
		{
			v[x - 1][y] = v[x][y] + 1;
			qx[r] = x - 1;
			qy[r++] = y;
			if (x - 1 == 4 && y == 4) return;
		}
		
		if (can(x + 1, y))
		{
			v[x + 1][y] = v[x][y] + 1;
			qx[r] = x + 1;
			qy[r++] = y;
			if (x + 1 == 4 && y ==4) return;
		}
		
		if (can(x, y - 1))
		{
			v[x][y - 1] = v[x][y] + 1;
			qx[r] = x;
			qy[r++] = y - 1;
			if (x == 4 && y - 1 == 4) return;
		}
		
		if (can(x, y + 1))
		{
			v[x][y + 1] = v[x][y] + 1;
			qx[r] = x;
			qy[r++] = y + 1;
			if (x == 4 && y + 1 == 4) return;
		}
		
	}
	
}
void dfs(int x, int y)
{
	if (x != 0 || y != 0)
	{
		if (x > 0 && v[x - 1][y] == v[x][y] - 1) dfs(x - 1, y); 
		if (x < 4 && v[x + 1][y] == v[x][y] - 1) dfs(x + 1, y); 
		if (y > 0 && v[x][y - 1] == v[x][y] - 1) dfs(x, y - 1); 
		if (y < 4 && v[x][y + 1] == v[x][y] - 1) dfs(x, y + 1);
	}
	
	printf("(%d, %d)\n", x, y); 
}
int main()
{
	for (int i = 0; i < 5; i++) 
	for (int j = 0; j < 5; j++)
		scanf("%d", &a[i][j]); 
	
    bfs();
//    for (int i = 0; i < 5; i++)
//    { 
//	for (int j = 0; j < 5; j++)
//     cout << v[i][j] << ' ';
//     cout << endl;
//    }
    dfs(4,4);
	return 0;
}


你可能感兴趣的:(搜索)