3 条题解

  • 0
    @ 2024-4-12 21:04:40

    DFS

    #include <iostream>
    
    using namespace std;
    
    const int N = 1010;
    
    char s[] = "tencent";
    
    int n, m;
    char d[N][N];
    bool st[N][N];
    int ans;
    
    int dx[] = { -1, 0, 1, 0 };
    int dy[] = { 0, -1, 0, 1 };
    
    void dfs(int x, int y, int u)
    {
    	if (u == 6)
    	{
    		ans++;
    		return;
    	}
    
    	for (int i = 0; i < 4; i++)
    	{
    		int nx = x + dx[i], ny = y + dy[i];
    		if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && d[nx][ny] == s[u + 1])
    			dfs(nx, ny, u + 1);
    	}
    }
    
    void solve()
    {
    	cin >> n >> m;
    	for (int i = 1; i <= n; i++)
    		for (int j = 1; j <= m; j++)
    			cin >> d[i][j];
    
    	for (int i = 1; i <= n; i++)
    		for (int j = 1; j <= m; j++)
    			if (d[i][j] == 't')
    				dfs(i, j, 0);
    		
    	cout << ans << endl;
    }
    
    int main()
    {
    	solve();
    	return 0;
    }
    

    信息

    ID
    81
    时间
    1000ms
    内存
    256MiB
    难度
    7
    标签
    递交数
    96
    已通过
    19
    上传者