POJ 3009

水题,连题目描述都不想写了

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
using namespace std;
int w, h, sx, sy, tx, ty, cnt;
int mov[4][2] = {{1,0},{0,-1},{-1,0},{0,1}};
int map[40][40];
void init()
{
	memset(map, 0, sizeof(map));
	sx = -1, sy = -1, tx = -1, ty = -1; 
	for (int i = 1; i <= h; i++)
	for (int j = 1; j <= w; j++)
	{
		int t;
		scanf("%d", &t);
		if (t == 0) map[i][j] = 0;
		if (t == 1) map[i][j] = 1;
		if (t == 2) {map[i][j] = 2; sx = i; sy = j;}
		if (t == 3) {map[i][j] = 3; tx = i; ty = j;}
	}
}
bool can (int xx, int yy)
{
	if ((xx <= 0) || (xx > h)) return false;
	if ((yy <= 0) || (yy > w)) return false;
	return true;
}
void dfs(int x, int y, int dep)
{
	if (dep > 10) return;
	for (int i = 0; i < 4; i++)
	{
		int xx = x + mov[i][0];
		int yy = y + mov[i][1];
	    int flag; 
		while(1)
		{
			if(xx == tx && yy == ty) {flag = -1;break;}
			if(map[xx][yy] == 1) {flag = 1;break;}	
			if(!can(xx, yy)) {flag = 0;break;}
			xx += mov[i][0];
			yy += mov[i][1];
		}
        
		if (flag == 1)
		{
			if ((xx - mov[i][0] == x) && (yy - mov[i][1] == y)) continue; 
			map[xx][yy] = 0;
			dfs(xx - mov[i][0], yy - mov[i][1], dep + 1);
			map[xx][yy] = 1;
		}
		if (flag == -1)
		{
			if (cnt > dep + 1) cnt = dep + 1; return;
		}
	}
}
int main()
{
	while (scanf("%d%d", &w, &h) != EOF  && w + h != 0)
	{
		init();
		cnt = 12;
		dfs(sx, sy, 0);
		if (cnt > 10) printf("%d\n", -1);
		else printf("%d\n", cnt);
	}
	return 0;
}


POJ 1321

更加简单版的类似八皇后问题

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <stdlib.h>
#include <math.h>
using namespace std;
int n, k, cnt = 0;
char map[10][10];
bool b[10];
void dfs(int x, int dep)
{
	for(int i = 0; i < n;i++)  
    {  
        if(map[x][i] == '#' && b[i] == 0)  
        {  
            if(dep == 1) cnt++;
            else  
            {  
                b[i]=1;  
                for(int j = x + 1; j < n - dep + 2; j++)  
                    dfs(j, dep-1);  
                b[i]=0;  
            }  
        }  
    }  
	
}
int main()
{
	while (scanf("%d%d", &n, &k) != EOF && n != - 1 && k !=- 1)
	{
		cnt = 0;
		for (int i = 0; i < n; i++)
		{
			scanf("%s" , map[i]);
		}
		for(int i = 0; i <= n - k; i++)   
        {  
            dfs(i, k); 
        }
        printf("%d\n", cnt);  
	}
	return 0;
}

POJ 1190

汉语题,不描述了啊

原来做过……

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <stdlib.h>
#include <math.h>
using namespace std;
#define INF 0xffffff
int mins[40], minv[40], n, m, ans; 
void dfs(int v, int s, int x, int r, int h)  
{  
    if(x == 0)  
    {  
        if(v == n && s < ans) ans = s;  
        return;  
    }  
    if(v+minv[x-1]>n || s+mins[x-1]>ans || s+2*(n-v)/r>=ans) return;  
  
    for(int i = r - 1; i >= x; i--)  
    {  
        if(x == m)  s = i * i;  
        int tmp = min((n - v - minv[x - 1]) / (i * i), h - 1);  
        for(int j = tmp; j >= x; j--)  
            dfs(v + i * i * j, s + 2 * i * j, x - 1, i, j);  
    }  
}  
int main()
{
	scanf("%d%d",  &n, &m);
	mins[0] = minv[0] = 0;  
    for(int i = 1; i <= m; i++)  
    {  
        minv[i] = minv[i - 1] + i * i * i;  
        mins[i] = mins[i - 1] + 2 * i * i;  
    }  
    int maxh = (n - minv[m - 1])/(m * m);
    int maxr = sqrt(1.0 * (n - minv[m - 1]) / m);
    ans = INF;  
    dfs(0, 0, m, maxr + 2, maxh + 2);  
    if(ans == INF)  ans = 0;  
    printf("%d\n", ans);  
	return 0;
}

好吧,我不是发题解,只是存题而已……大家无视