poj1548Robots dfs做法

//搜索每一行

//将该行的所有点都清除

//然后再一改行的最后一个点的位置向下走一步

//然后将下面一行的所有点清除

//然后再重复上述操作

#include<iostream>

#include<cstdio>

#include<cstring>

using namespace std;

const int maxn=30;

int line[maxn][maxn];

int C,R;

int ans = 0;

void dfs(int x , int y)

{

int pos = y;

for(int j = y ;j <= C ;j++)

if(line[x][j])

{

line[x][j] = 0;

pos = j;

}

if(x == R)return ;

dfs(x+1 , pos);

}

int main()

{

int a,b;

//freopen("in.txt","r",stdin);

while(scanf("%d%d", &a ,&b)&&(a != -1 && b != -1))

{

memset(line, 0 ,sizeof(line));

if(!a && !b)

{

printf("0\n");

continue;

}

C = R = 0;

while(a && b)

{

C = max(C , b);

R = max(R , a);

line[a][b] = 1;

scanf("%d %d",&a ,&b);

}

ans = 0;

for(int i = 1 ;i <= R ;i++)

for(int j = 1 ;j<=C ;j++)

if(line[i][j])

{

dfs(i,j);

ans++;

}

printf("%d\n" , ans);

}

return 0;

}

时间: 2024-09-30 06:33:42

poj1548Robots dfs做法的相关文章

poj 1724ROADS(bfs和dfs做法)

1 /* 2 dfs比较好想,就是测试数据的问题,导致在遍历边的时候要倒着遍历才过! 3 */ 4 #include<iostream> 5 #include<cstdio> 6 #include<cstring> 7 #include<vector> 8 #include<algorithm> 9 #define Max 0x3f3f3f3f 10 using namespace std; 11 12 struct node{ 13 int D

HDU--1198 Farm Irrigation (并查集做法+DFS做法)

Problem Description Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot of samll squares. Water pipes are placed in these squares. Different square has a different type of pipe. There are 11 types of pi

poj1548Robots dfs实践

//搜索每一行 //该生产线的整点已被清除 //然后位置,然后转移到下一个步走的最后一点 //然后,所有点的下面一行清晰 //然后重复上面的操作 #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn=30; int line[maxn][maxn]; int C,R; int ans = 0; void dfs(int x , int y) {

全排列的DFS做法

#include<iostream> #include<cstring> using namespace std; const int maxn=1e3; int vis[maxn]; int p[maxn]; int n; int t=0; void dfs(int x) { if(x==n+1) { for(int i=1;i<=n;i++) cout<<p[i]<<" "; cout<<endl; return ;

蓝桥杯竞赛题《地宫取宝》DP做法

问题描述 X 国王有一个地宫宝库.是 n x m 个格子的矩阵.每个格子放一件宝贝.每个宝贝贴着价值标签. 地宫的入口在左上角,出口在右下角. 小明被带到地宫的入口,国王要求他只能向右或向下行走. 走过某个格子时,如果那个格子中的宝贝价值比小明手中任意宝贝价值都大,小明就可以拿起它(当然,也可以不拿). 当小明走到出口时,如果他手中的宝贝恰好是k件,则这些宝贝就可以送给小明. 请你帮小明算一算,在给定的局面下,他有多少种不同的行动方案能获得这k件宝贝. 输入格式 输入一行3个整数,用空格分开:n

hdu 1242 dfs

Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is: approa

133. Clone Graph

题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each n

codeforces 几道题目

BZOJ挂了....明天就要出发去GDKOI了....不能弃疗. 于是在cf水了几道题, 写写详(jian)细(dan)题解, 攒攒RP, 希望GDKOI能好好发挥.......  620E. New Year Tree 题目大意: N个结点的树, 结点1为根, 要支持2种操作(M个操作): 1.将以v为根的子树所有节点的颜色为c 2.询问以v为根的子树中不同颜色个数 N,M<=4*10^5, 1<=c<=60 题解: 处理出dfs序, 线段树维护. 1,2操作都对应线段树的一段区间(子

拓扑排序入门

10305 - Ordering Tasks John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task is only possible if other tasks have already been executed. Input The input will consist of several instances of the problem. Ea