阿里4.10号笔试第二题

求各位大神看看这个dfs怎么通过0.。。。

#include
using namespace std;
int n, m, c, t;
int vis[50][50];
vectorv;
int dir[4][2] = { 0,1,1,0,0,-1,-1,0 };
int dfs(int x, int y)
{
 if (x == n + 1)
 {
  /*for (int i = 1; i <= n; i++)
   for (int j = 1; j <= m; j++)
    cout << vis[i][j] << (j == m ? '\n' : ' ');*/
  return 1;
 }
 for (int i = 1; i <= c; i++)
 {
  if (v[i])
  {
   int flag = 0;
   for (int j = 0; j < 4; j++)
   {
    int xx = x + dir[j][0];
    int yy = y + dir[j][1];
    if (vis[xx][yy] == i)
    {
     flag = 1;
     break;
    }
   }
   if (flag)continue;
   vis[x][y] = i;
   v[i]--;
   int new_x = x;
   int new_y = y+1;
   if (new_y == m+1) new_x++, new_y = 1;
   int ans = dfs(new_x, new_y);
   vis[x][y] = 0;
   v[i]++;

   if (ans) return 1;
  }
 }
 return 0;
}
int main()
{
 //ifstream cin("input.txt");
 cin >> t;
 while (t--)
 {
  cin >> n >> m>> c;
  v.clear();
  memset(vis, 0, sizeof vis);
  v.push_back(0);
  for (int i = 1; i <= c; i++)
  {
   int tem;
   cin >> tem;
   v.push_back(tem);
  }
  cout << (dfs(1, 1) ? "YES" : "NO") << endl;
 }
}
#阿里笔试410号##阿里巴巴##笔试题目#
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-08 13:05
点赞 评论 收藏
分享
06-17 00:26
门头沟学院 Java
程序员小白条:建议换下项目,智能 AI 旅游推荐平台:https://github.com/luoye6/vue3_tourism_frontend 智能 AI 校园二手交易平台:https://github.com/luoye6/vue3_trade_frontend GPT 智能图书馆:https://github.com/luoye6/Vue_BookManageSystem 选项目要选自己能掌握的,然后最好能自己拓展的,分布式这种尽量别去写,不然你只能背八股文了,另外实习的话要多投,尤其是学历不利的情况下,多找几段实习,最好公司title大一点的
无实习如何秋招上岸
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

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