CodeForce 540C:(DFS)

终点必须是X时才能完成,如果是“。"则意味着终点需要走两次

用mat[i][j]表示该点还能经过的次数

#include"cstdio"
#include"cstring"
#include"queue"
#include"iostream"
#define MAXN 505
using namespace std;
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
int mat[MAXN][MAXN];
int x,y,ok,mark;
char arr[MAXN];
struct point
{   int x;
    int y;
}s,e,temp;
bool dfs(point s)
{   //cout<<s.x<<‘\t‘<<s.y<<endl;
    if(s.x==e.x&&s.y==e.y&&!mat[s.x][s.y]) {ok=1;return true;}
    if(ok) return true;
    for(int k=0;k<4;k++){
        temp.x=s.x+dx[k];
        temp.y=s.y+dy[k];
        //cout<<mat[temp.x][temp.y]<<‘\t‘<<endl;
        if(temp.x>=0&&temp.x<x&&temp.y>=0&&temp.y<y&&mat[temp.x][temp.y]){
            mat[temp.x][temp.y]--;
            if(dfs(temp)) return true;
        }
    }
    return false;
}
int main()
{   while(scanf("%d%d",&x,&y)!=EOF){
        memset(mat,0,sizeof(mat));
        for(int i=0;i<x;i++){
            scanf("%s",arr);
            for(int j=0;j<y;j++){
                if(arr[j]==‘.‘) mat[i][j]=1;
                else mat[i][j]=0;
            }
        }
        scanf("%d%d",&s.x,&s.y);s.x--;s.y--;
        scanf("%d%d",&e.x,&e.y);e.x--;e.y--;
        ok=0;
        mark=1;
        //mat[s.x][s.y]=1;
        mat[e.x][e.y]++;
        dfs(s);
        if(ok) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
时间: 2024-08-02 17:02:52

CodeForce 540C:(DFS)的相关文章

codeforce Pashmak and Buses(dfs枚举)

1 /* 2 题意:n个同学,k个车, 取旅游d天! 3 要求所有的学生没有两个或者两个以上的在同一辆车上共同带d天! 输出可行的方案! 4 5 对于d行n列的矩阵,第i行第j列表示的是第i天第j个同学所在的车号! 6 也就是保证所有行不全相同,即每一列都是不相同的! 7 如果每一列都不相同就是表示第j个同学(第j列)在这d天中不会和其他同学(列)在这d天中 都在同一辆车中! 8 9 思路:对于每一列我们枚举d天该学生所在的车号!它的下一列只保证有一个元素和它不同就行了!依次下去! 10 11

CodeForce 117C Cycle DFS

A tournament is a directed graph without self-loops in which every pair of vertexes is connected by exactly one directed edge. That is, for any two vertexes u and v (u ≠ v) exists either an edge going from u to v, or an edge from v to u. You are give

codeforce 124B——全排列dfs——Permutations

You are given nk-digit integers. You have to rearrange the digits in the integers so that the difference between the largest and the smallest number was minimum. Digits should be rearranged by the same rule in all integers. Input The first line conta

codeforce 14D 无向图 求树的长度 bfs是一种方法 先用dfs做

枚举每一条边  将树分为两部分 分别dfs求出树的直径 从一点开始最长路加次长路为树的直径 #include<iostream> #include<vector>#include<algorithm>using namespace std;vector<int> G[100000];int n,s;inline int max(int a,int b)//注意是inline 不然超时{       return a>b?a:b;}int dfs(int

CodeForce 377 A mazes(dfs+连通性问题)

Pavel 喜欢网格迷宫.一个网格迷宫是一个 n × m 的长方形迷宫,其中每个单元格要么是空白的,要么是墙体.您可以从一个单元格走到另一个单元格,只要两个单元格均是空白的,且拥有一条公共的边. Pavel 绘制了一个网格迷宫,包含的全部空白单元格形成了一个连通区域.换言之,您可以从任何一个空白的单元格,走到其它任意的空白单元格.Pavel 的迷宫如果墙体太少,他就不喜欢这个迷宫.他希望将 k 个空白的单元格转换为墙体,使得剩余的全部单元格仍然能够形成一个连通区域.请帮助他实现这个任务. 输入

CodeForce 448C 木片填涂问题

题目大意:有多片木片需要填涂,可以每次横着涂一行,也可以一次涂一列,当然你涂一行时遇到中间长度不够高的木片,填涂到此中断 这题目运用dfs能更容易的解出,虽然还是十分不容易理解 1 #include <iostream> 2 3 using namespace std; 4 5 #define N 5010 6 int a[N],n; 7 8 int Min(int c,int d) 9 { 10 return c<d?c:d; 11 } 12 int dfs(int c,int d,i

Codeforce 9C - Hexadecimal&#39;s Numbers

One beautiful July morning a terrible thing happened in Mainframe: a mean virus Megabyte somehow got access to the memory of his not less mean sister Hexadecimal. He loaded there a huge amount of n different natural numbers from 1 to n to obtain tota

CodeForces 540C Ice Cave (BFS)

http://codeforces.com/problemset/problem/540/C Ice Cave Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 540C Description You play a computer game. Your character stands on some level of

解救小哈——DFS算法举例

一.问题引入 有一天,小哈一个人去玩迷宫.但是方向感不好的小哈很快就迷路了.小哼得知后便去解救无助的小哈.此时的小哼已经弄清楚了迷宫的地图,现在小哼要以最快的速度去解救小哈.那么,问题来了... 二.问题的分析 首先我们用一个二维数组来存储这个迷宫,刚开始的时候,小哼处于迷宫的入口处(1,1),小哈在(p,q).其实这道题的的本质就在于找从(1,1)到(p,q)的最短路径. 此时摆在小哼面前的路有两条,我们可以先让小哼往右边走,直到走不通的时候再回到这里,再去尝试另外一个方向. 在这里我们规定一