2015 UESTC Winter Training 中的简单搜索


uvalive 4997

简答搜索

先将中心求出来,并且染上无关色,染色之后若有地方未染色,则说明不行

然后再用回溯法染色

//      whn6325689
//		Mr.Phoebe
//		http://blog.csdn.net/u013007900
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#include <functional>
#include <numeric>
#pragma comment(linker, "/STACK:1024000000,1024000000")


using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef vector<int> vi;

#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define lowbit(x) (x&(-x))
#define MID(x,y) (x+((y-x)>>1))
#define eps 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LLINF 1LL<<62

template<class T>
inline bool read(T &n)
{
    T x = 0, tmp = 1;
    char c = getchar();
    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
    if(c == EOF) return false;
    if(c == '-') c = getchar(), tmp = -1;
    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
    n = x*tmp;
    return true;
}
template <class T>
inline void write(T n)
{
    if(n < 0)
    {
        putchar('-');
        n = -n;
    }
    int len = 0,data[20];
    while(n)
    {
        data[len++] = n%10;
        n /= 10;
    }
    if(!len) data[len++] = 0;
    while(len--) putchar(data[len]+48);
}
//-----------------------------------

const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
const int DX[] = {-2, -2, -2, -1, -1, -1, -1,  0, 0,  1,  1, 1, 1,  2, 2, 2};
const int DY[] = {-1,  0,  1, -2, -1,  1,  2, -2, 2, -2, -1, 1, 2, -1, 0, 1};

int n;
char ma[20][20];
struct Node
{
    int x, y;
    char col;
    Node(){}
    Node(const int &x,const int &y,const char col='\0'):x(x),y(y),col(col){}
} a[400];
int tot;
bool flag;

bool isCenter(int x, int y)
{
    if(ma[x][y] != '.') return 0;
    for(int k=0; k<4; k++)
    {
        int xx=x+dx[k];
        int yy=y+dy[k];
        if(xx<1 || xx>n || yy<1 || yy>n)
            return 0;
        if(ma[xx][yy]!='.')
            return 0;
    }
    return 1;
}
void change(int x,int y,char ch)
{
    ma[x][y]=ch;
    for(int k=0; k<4; k++)
        ma[x+dx[k]][y+dy[k]]=ch;
}

bool isfull()
{
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            if(ma[i][j]=='.')
                return 0;
    return 1;
}

bool check(int x,int y,char ch)
{
    for(int k=0; k<16; k++)
    {
        int xx=x+DX[k];
        int yy=y+DY[k];
        if(xx<1 || xx>n || yy<1 || yy>n) continue;
        if(ma[xx][yy]==ch) return 0;
    }
    return 1;
}

void dfs(int x)
{
    if(flag) return;
    if(x>tot)
    {
        flag=1;
        return;
    }
    for(char ch='B'; ch<='D'; ch++)
    {
        if(!check(a[x].x,a[x].y,ch))  continue;
        a[x].col=ch;
        change(a[x].x,a[x].y,ch);
        dfs(x+1);
        if(flag)
            return;
    }
    change(a[x].x,a[x].y,'q');
}

int main()
{
    int T,cas=1;
    read(T);
    while(T--)
    {
        read(n);
        printf("Case %d:",cas++);
        for(int i=1; i<=n; i++)
            scanf("%s", ma[i]+1);
        tot=0;
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                if(isCenter(i, j))
                {
                    a[++tot]=Node(i,j);
                    change(i,j,'q');
                }
        if(!isfull())
            puts(" Not Possible!");
        else
        {
            flag=0;
            dfs(1);
            if(!flag)
                puts(" Not Possible!");
            else
            {
                putchar('\n');
                for(int i=1; i<=n; i++)
                    printf("%s\n",ma[i]+1);
            }
        }
    }
    return 0;
}




uvalive 5066

bfs求最短路

然后用背包求答案

//      whn6325689
//		Mr.Phoebe
//		http://blog.csdn.net/u013007900
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#include <functional>
#include <numeric>
#pragma comment(linker, "/STACK:1024000000,1024000000")


using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef vector<int> vi;

#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define lowbit(x) (x&(-x))
#define MID(x,y) (x+((y-x)>>1))
#define eps 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LLINF 1LL<<62

template<class T>
inline bool read(T &n)
{
    T x = 0, tmp = 1; char c = getchar();
    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
    if(c == EOF) return false;
    if(c == '-') c = getchar(), tmp = -1;
    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
    n = x*tmp;
    return true;
}
template <class T>
inline void write(T n)
{
    if(n < 0)
    {
        putchar('-');
        n = -n;
    }
    int len = 0,data[20];
    while(n)
    {
        data[len++] = n%10;
        n /= 10;
    }
    if(!len) data[len++] = 0;
    while(len--) putchar(data[len]+48);
}
//-----------------------------------

struct Node
{
    int x,y,z;
    int step;
    Node(const int& x=0,const int& y=0,const int& z=0,const int& step=0):x(x),y(y),z(z),step(step){}
};
struct P
{
    int ti,sc;
}pp[111];
const int dx[]={0,0,0,0,1,-1},dy[]={1,-1,0,0,0,0},dz[]={0,0,1,-1,0,0};
char ma[15][111][111];
bool vis[15][111][111];
int dp[10010],p[15][111][111];
int l,h,w,n,s,tot;
Node st;
queue<Node> q;

bool in(int x,int y,int z)
{
    if (x>=1 && x<=l && y>=1 && y<=h && z>=1 && z<=w && ma[x][y][z]!='X' && vis[x][y][z]==0)
		return 1;
    return 0;
}

void bfs()
{
	int x,y,z;
	CLR(vis,0);
	while(!q.empty())	q.pop();
	q.push(st);
    vis[st.x][st.y][st.z]=1;
    tot=0;
    while(!q.empty())
    {
    	Node now=q.front();
    	q.pop();
    	if(ma[now.x][now.y][now.z]=='#')
		{
            pp[++tot].sc=p[now.x][now.y][now.z];
            pp[tot].ti=now.step*3;
        }
    	for(int i=0;i<4;i++)
    	{
            x=now.x+dx[i];
            y=now.y+dy[i];
            z=now.z+dz[i];
            if(in(x,y,z))
			{
                q.push(Node(x,y,z,now.step+1));
                vis[x][y][z]=1;
            }
        }
        if(ma[now.x][now.y][now.z]=='U')
		{
            x=now.x+dx[4];
            y=now.y+dy[4];
            z=now.z+dz[4];
            if(in(x,y,z))
			{
                q.push(Node(x,y,z,now.step+1));
                vis[x][y][z]=1;
            }
        }
        if(ma[now.x][now.y][now.z]=='D')
		{
            x=now.x+dx[5];
            y=now.y+dy[5];
            z=now.z+dz[5];
            if(in(x,y,z))
			{
  				q.push(Node(x,y,z,now.step+1));
                vis[x][y][z]=1;
            }
        }	
    }
	
}

int main()
{
//	freopen("data.txt","r",stdin);
	int T;
	scanf("%d",&T);
	while(T--)
	{
		CLR(dp,0);CLR(pp,0);
  		scanf("%d %d %d %d %d",&l,&h,&w,&n,&s);
		for(int i=1;i<=l;i++)
            for(int j=1;j<=h;j++)
			{
                scanf("%s",ma[i][j]+1);
                for(int k=1;k<=w;k++)
                    if(ma[i][j][k]=='S')
						st=Node(i,j,k,0);                    
            }
        for(int i=1,fl,hh,ww,sc;i<=n;i++)
		{
            scanf("%d %d %d %d",&fl,&hh,&ww,&sc);
            p[fl][hh][ww]=sc;
            ma[fl][hh][ww]='#';
        }
/*
        for(int i=1;i<=l;i++)
        	for(int j=1;j<=h;j++)
        		printf("%s\n",ma[i][j]+1);
*/
		bfs();
		for(int i=1;i<=tot;i++)
            for(int j=s;j>=pp[i].ti;j--)
                dp[j]=max(dp[j],dp[j-pp[i].ti]+pp[i].sc);
        printf("%d\n",dp[s]);
	}
	return 0;
}


HDU 4166

bfs暴搜

本来想用A*的,后来想起来还要求路径

//      whn6325689
//		Mr.Phoebe
//		http://blog.csdn.net/u013007900
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#include <functional>
#include <numeric>
#pragma comment(linker, "/STACK:1024000000,1024000000")


using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef vector<int> vi;

#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define lowbit(x) (x&(-x))
#define MID(x,y) (x+((y-x)>>1))
#define eps 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LLINF 1LL<<62

template<class T>
inline bool read(T &n)
{
    T x = 0, tmp = 1; char c = getchar();
    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
    if(c == EOF) return false;
    if(c == '-') c = getchar(), tmp = -1;
    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
    n = x*tmp;
    return true;
}
template <class T>
inline void write(T n)
{
    if(n < 0)
    {
        putchar('-');
        n = -n;
    }
    int len = 0,data[20];
    while(n)
    {
        data[len++] = n%10;
        n /= 10;
    }
    if(!len) data[len++] = 0;
    while(len--) putchar(data[len]+48);
}
//-----------------------------------

int n,m,mod;

const int dir[4][2]={-1,0,0,1,1,0,0,-1};

struct Node
{
	int x,y,dir;
	int step;
	Node(const int& x=0,const int& y=0,const int& dir=0,const int& step=0):x(x),y(y),dir(dir),step(step){}
	bool operator == (const Node& b) const
	{
		if(x==b.x && y==b.y)	return true;
		return false;
	}
	void output()
	{
		write(x),putchar(' '),write(y),putchar(' '),write(dir),putchar('\n');
	}
};

int dis[1010][1010][4],num[1010][1010][4];
char ma[1010][1010],ss[22];
int idx[200],flag,ans;

bool in(int x,int y)
{
	return x>=0 && x<n && y>=0 && y<m && ma[x][y]=='.';
}

void bfs(Node s,Node t)
{
	queue<Node > q;
	int f[3];
	while(!q.empty())	q.pop();
	dis[s.x][s.y][s.dir]=0;
	num[s.x][s.y][s.dir]=1;
	q.push(s);
	while(!q.empty())
	{
		Node now=q.front();q.pop();
		int x=now.x,y=now.y;
		f[0]=now.dir;f[1]=(f[0]+1)%4;f[2]=(f[0]+3)%4;
		int xx=x+dir[f[0]][0];
		int yy=y+dir[f[0]][1];
		if(now==t)
		{
			flag=1;
			for(int i=0;i<4;i++)
				if(dis[x][y][i]==now.step)
					ans=(ans+num[x][y][i])%mod; 
			break;
		}
		if(in(xx,yy))
		{
			if(dis[xx][yy][f[0]]>now.step+1)
			{
				dis[xx][yy][f[0]]=now.step+1;
				num[xx][yy][f[0]]=num[x][y][f[0]];
				q.push(Node(xx,yy,f[0],dis[xx][yy][f[0]]));
			}
			else if(dis[xx][yy][f[0]]==now.step+1)
				num[xx][yy][f[0]]=(num[xx][yy][f[0]]+num[x][y][f[0]])%mod;
		}
		for(int i=1;i<=2;i++)
		{
			if(dis[x][y][f[i]]>now.step+1)
			{
				dis[x][y][f[i]]=now.step+1;
				num[x][y][f[i]]=num[x][y][f[0]];
				q.push(Node(x,y,f[i],dis[x][y][f[i]]));
			}
			else if(dis[x][y][f[i]]==now.step+1)
				num[x][y][f[i]]=(num[x][y][f[i]]+num[x][y][f[0]])%mod;
		}
	} 
}

int main()
{
//	freopen("data.txt","r",stdin);
	int cas=1;
	idx['N']=0;idx['E']=1;
    idx['S']=2;idx['W']=3;
    while(read(n)&&read(m)&&read(mod)&&(mod))
    {
    	CLR(dis,INF);CLR(num,0);
    	Node s,t;flag=ans=0;
    	for(int i=0;i<n;i++)
    		scanf("%s",ma[i]);
   		read(s.x),read(s.y);
   		read(t.x),read(t.y);
   		scanf("%s",ss);
   		s.dir=idx[ss[0]];s.step=0;
   		bfs(s,t);
   		if(!flag)
   			ans=-1;
		printf("Case %d: %d %d\n",cas++,mod,ans);
    }
    return 0;
}


你可能感兴趣的:(2015 UESTC Winter Training 中的简单搜索)