牛客寒假算法基础集训营5 I. 炫酷镜子(模拟)

 

题目链接:https://ac.nowcoder.com/acm/contest/331/I

       按找题意模拟就好了...代码略丑,不想改了...


AC代码:

#include <bits/stdc++.h>
using namespace std;
string str[505];
int n,m,pos;
bool flag;
 
void dfs(int x,int y,int h){
  while(1){
    if(x == -1 || y == -1 || x == n || y == m){
      if(x == n) flag = true, pos = y + 1;
      break;
    }
    // cout<<x<<"  "<<y<<endl;
    while(x >= 0 && y >= 0 && x < n && y < m && str[x][y] != '.'){
      // cout<<x<<"  "<<y<<endl;
      if(str[x][y] == '/'){
        if(h == 0){ y = y - 1, h = 2; continue;}
        if(h == 1){ y = y + 1, h = 3; continue;}
        if(h == 2){ x = x + 1, h = 0; continue;}
        if(h == 3){ x = x - 1, h = 1; continue;}
        // continue;
      }
      if(str[x][y] == '\\'){
        if(h == 0){ y = y + 1; h = 3; continue;}
        if(h == 1){ y = y - 1; h = 2; continue;}
        if(h == 2){ x = x - 1; h = 1; continue;}
        if(h == 3){ x = x + 1; h = 0; continue;}
      }
      // cout<<x<<"  "<<y<<endl;
    }
    // cout<<x<<"  "<<y<<endl;
    if(x >= 0 && y >= 0 && x < n && y < m && str[x][y] == '.'){
      if(h == 0)x = x + 1;
      if(h == 1)x = x - 1;
      if(h == 2)y = y - 1;
      if(h == 3)y = y + 1;
    }
  }
}
 
int main()
{
  scanf("%d%d",&n,&m);
  for(int i=0;i<n;i++){
    cin>>str[i];
  }
  for(int i=0;i<m;i++){
    flag = false;
    dfs(0,i,0);
    if(flag == true)cout<<pos<<endl;
    else puts("-1");
  }
  return 0;
}

 

全部评论

相关推荐

AI牛可乐:哇,听起来你很激动呢!杭州灵枢维度科技听起来很厉害呀~你逃课去白马培训,老冯会同意吗?不过既然你这么感兴趣,肯定是有原因的吧! 对了,想了解更多关于这家公司或者求职相关的问题吗?可以点击我的头像私信我哦,我可以帮你更详细地分析一下!
你都用vibe codi...
点赞 评论 收藏
分享
好羡慕走AI技术方向的人,实习薪资这么高😱&nbsp;人比人气死人😭
勇敢的嘟教授在考古:芯片公司挣得多多的,钱给的还不如互联网
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务